exec_outcome
stringclasses 1
value | code_uid
stringlengths 32
32
| file_name
stringclasses 111
values | prob_desc_created_at
stringlengths 10
10
| prob_desc_description
stringlengths 63
3.8k
| prob_desc_memory_limit
stringclasses 18
values | source_code
stringlengths 117
65.5k
| lang_cluster
stringclasses 1
value | prob_desc_sample_inputs
stringlengths 2
802
| prob_desc_time_limit
stringclasses 27
values | prob_desc_sample_outputs
stringlengths 2
796
| prob_desc_notes
stringlengths 4
3k
⌀ | lang
stringclasses 5
values | prob_desc_input_from
stringclasses 3
values | tags
sequencelengths 0
11
| src_uid
stringlengths 32
32
| prob_desc_input_spec
stringlengths 28
2.37k
⌀ | difficulty
int64 -1
3.5k
⌀ | prob_desc_output_spec
stringlengths 17
1.47k
⌀ | prob_desc_output_to
stringclasses 3
values | hidden_unit_tests
stringclasses 1
value |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED | 59976c2c960b2b6af93e4c0728489f4a | train_110.jsonl | 1618839300 | Given an array $$$a$$$ of length $$$n$$$, tell us whether it has a non-empty subsequence such that the product of its elements is not a perfect square.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly zero) elements. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Double.parseDouble;
import static java.lang.Integer.compare;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.System.in;
import static java.lang.System.out;
import static java.util.Arrays.asList;
import static java.util.Collections.max;
import static java.util.Collections.min;
public class Main {
private static final int MOD = (int) (1E9 + 7);
private static final int INF = (int) (1E9);
static FastScanner scanner = new FastScanner(in);
public static void main(String[] args) throws IOException {
// Write your solution here
StringBuilder answer = new StringBuilder();
int t = 1;
t = parseInt(scanner.nextLine());
while (t-- > 0) {
answer.append(solve()).append("\n");
}
out.println(answer);
}
private static Object solve() throws IOException {
int n = scanner.nextInt();
Integer[] a = new Integer[n];
int not = 0;
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
if(!isSquare(a[i])) not++;
}
if(not > 0) return "YES";
return "NO";
}
private static boolean isSquare(int n){
int s = (int) Math.sqrt(n);
return s * s == n;
}
private static class Pair implements Comparable<Pair> {
int index, value;
public Pair(int index, int value) {
this.index = index;
this.value = value;
}
public int compareTo(Pair o) {
if (value != o.value) return compare(value, o.value);
return compare(index, o.index);
}
}
private static int maxx(Integer... a) {
return max(asList(a));
}
private static int minn(Integer... a) {
return min(asList(a));
}
private static long maxx(Long... a) {
return max(asList(a));
}
private static long minn(Long... a) {
return min(asList(a));
}
private static int add(int a, int b) {
long res = ((long) (a + MOD) % MOD + (b + MOD) % MOD) % MOD;
return (int) res;
}
private static int mul(int a, int b) {
long res = ((long) a % MOD * b % MOD) % MOD;
return (int) res;
}
private static int pow(int a, int b) {
a %= MOD;
int res = 1;
while (b > 0) {
if ((b & 1) != 0)
res = mul(res, a);
a = mul(a, a);
b >>= 1;
}
return res;
}
private static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
return null;
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return parseInt(next());
}
long nextLong() {
return parseLong(next());
}
double nextDouble() {
return parseDouble(next());
}
}
} | Java | ["2\n3\n1 5 4\n2\n100 10000"] | 1 second | ["YES\nNO"] | NoteIn the first example, the product of the whole array ($$$20$$$) isn't a perfect square.In the second example, all subsequences have a perfect square product. | Java 8 | standard input | [
"math",
"number theory"
] | 33a31edb75c9b0cf3a49ba97ad677632 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_{n}$$$ ($$$1 \le a_i \le 10^4$$$) — the elements of the array $$$a$$$. | 800 | If there's a subsequence of $$$a$$$ whose product isn't a perfect square, print "YES". Otherwise, print "NO". | standard output | |
PASSED | b2d3ac2ee0ab7f1888ed07777b17c025 | train_110.jsonl | 1618839300 | Given an array $$$a$$$ of length $$$n$$$, tell us whether it has a non-empty subsequence such that the product of its elements is not a perfect square.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly zero) elements. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Double.parseDouble;
import static java.lang.Integer.compare;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.System.in;
import static java.lang.System.out;
import static java.util.Arrays.asList;
import static java.util.Collections.max;
import static java.util.Collections.min;
public class Main {
private static final int MOD = (int) (1E9 + 7);
private static final int INF = (int) (1E9);
static FastScanner scanner = new FastScanner(in);
public static void main(String[] args) throws IOException {
// Write your solution here
StringBuilder answer = new StringBuilder();
int t = 1;
t = parseInt(scanner.nextLine());
while (t-- > 0) {
answer.append(solve()).append("\n");
}
out.println(answer);
}
private static Object solve() throws IOException {
int n = scanner.nextInt();
Integer[] a = new Integer[n];
int not = 0;
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
if(!isSquare(a[i])) not++;
}
if(not > 0) return "YES";
return "NO";
}
private static boolean isSquare(int n){
int s = (int) Math.sqrt(n);
return s * s == n;
}
private static class Pair implements Comparable<Pair> {
int index, value;
public Pair(int index, int value) {
this.index = index;
this.value = value;
}
public int compareTo(Pair o) {
if (value != o.value) return compare(value, o.value);
return compare(index, o.index);
}
}
private static int maxx(Integer... a) {
return max(asList(a));
}
private static int minn(Integer... a) {
return min(asList(a));
}
private static long maxx(Long... a) {
return max(asList(a));
}
private static long minn(Long... a) {
return min(asList(a));
}
private static int add(int a, int b) {
long res = ((long) (a + MOD) % MOD + (b + MOD) % MOD) % MOD;
return (int) res;
}
private static int mul(int a, int b) {
long res = ((long) a % MOD * b % MOD) % MOD;
return (int) res;
}
private static int pow(int a, int b) {
a %= MOD;
int res = 1;
while (b > 0) {
if ((b & 1) != 0)
res = mul(res, a);
a = mul(a, a);
b >>= 1;
}
return res;
}
private static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
return null;
}
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return parseInt(next());
}
long nextLong() {
return parseLong(next());
}
double nextDouble() {
return parseDouble(next());
}
}
} | Java | ["2\n3\n1 5 4\n2\n100 10000"] | 1 second | ["YES\nNO"] | NoteIn the first example, the product of the whole array ($$$20$$$) isn't a perfect square.In the second example, all subsequences have a perfect square product. | Java 8 | standard input | [
"math",
"number theory"
] | 33a31edb75c9b0cf3a49ba97ad677632 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_{n}$$$ ($$$1 \le a_i \le 10^4$$$) — the elements of the array $$$a$$$. | 800 | If there's a subsequence of $$$a$$$ whose product isn't a perfect square, print "YES". Otherwise, print "NO". | standard output | |
PASSED | 8dc4e1c1bdde507c8a1d66ec06e8a1c0 | train_110.jsonl | 1618839300 | Given an array $$$a$$$ of length $$$n$$$, tell us whether it has a non-empty subsequence such that the product of its elements is not a perfect square.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly zero) elements. | 256 megabytes | //package com.govinda.codeforces.codeforces;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* # https://codeforces.com/problemset/problem/1517/A
* # A. Sum of 2050
*/
public class App
{
final static PrintWriter s_writer = new PrintWriter(System.out);
private static int count = 1;
private static class Reader
{
static StringTokenizer s_tokenizer = new StringTokenizer("");
private static BufferedReader s_reader;
private static interface Settings
{
static final boolean ONE_WORD_PER_LINE = false;
static final boolean CONSOLE_READ = true;
}
static
{
if (Settings.CONSOLE_READ)
s_reader = new BufferedReader(new InputStreamReader(System.in));
else
{
// read from file
try
{
s_reader = new BufferedReader(new InputStreamReader(new FileInputStream("Input.txt")));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
static String next() throws IOException
{
if (Settings.ONE_WORD_PER_LINE)
return s_reader.readLine();
while (!s_tokenizer.hasMoreTokens())
s_tokenizer = new StringTokenizer(s_reader.readLine());
return s_tokenizer.nextToken();
}
static int nextInt() throws IOException
{
return Integer.parseInt(next());
}
@SuppressWarnings("unused")
static double nextDouble() throws IOException
{
return Double.parseDouble(next());
}
static long nextLong() throws IOException
{
return Long.parseLong(next());
}
static int[] nextIntArray(int p_numElems) throws IOException
{
int[] result = new int[p_numElems];
for (int i = 0; i < p_numElems; i++)
result[i] = Reader.nextInt();
return result;
}
@SuppressWarnings("unused")
static void close()
{
s_writer.close();
}
}
private static void solve() throws IOException
{
int numInputs = Reader.nextInt();
while (numInputs-- > 0)
{
int numOfElems = Reader.nextInt();
int[] array = Reader.nextIntArray(numOfElems);
boolean result = false;
for (int i = 0; i < numOfElems; i++)
{
if (Math.sqrt(array[i]) % 1 != 0)
{
result = true;
break;
}
}
s_writer.println(result ? "YES" : "NO");
}
s_writer.close();
}
public static void printArray(int[] p_data)
{
s_writer.print(p_data[0]);
for (int i = 1; i < p_data.length; i++)
{
s_writer.print(" ");
s_writer.print(p_data[i]);
}
s_writer.println();
}
public static void main(String[] args) throws IOException
{
solve();
}
}
| Java | ["2\n3\n1 5 4\n2\n100 10000"] | 1 second | ["YES\nNO"] | NoteIn the first example, the product of the whole array ($$$20$$$) isn't a perfect square.In the second example, all subsequences have a perfect square product. | Java 8 | standard input | [
"math",
"number theory"
] | 33a31edb75c9b0cf3a49ba97ad677632 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_{n}$$$ ($$$1 \le a_i \le 10^4$$$) — the elements of the array $$$a$$$. | 800 | If there's a subsequence of $$$a$$$ whose product isn't a perfect square, print "YES". Otherwise, print "NO". | standard output | |
PASSED | 09b5edf8b77119b3dafdfa1518e0f5ca | train_110.jsonl | 1618839300 | Given an array $$$a$$$ of length $$$n$$$, tell us whether it has a non-empty subsequence such that the product of its elements is not a perfect square.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly zero) elements. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i < n; i++){
arr[i] = sc.nextInt();
}
boolean isFound = false;
for(int i = 0; i < n; i++){
int sr = (int)Math.sqrt(arr[i]);
if(sr*sr != arr[i]){
isFound = true;
break;
}
}
System.out.println(isFound==true?"YES": "NO");
}
}
}
| Java | ["2\n3\n1 5 4\n2\n100 10000"] | 1 second | ["YES\nNO"] | NoteIn the first example, the product of the whole array ($$$20$$$) isn't a perfect square.In the second example, all subsequences have a perfect square product. | Java 8 | standard input | [
"math",
"number theory"
] | 33a31edb75c9b0cf3a49ba97ad677632 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_{n}$$$ ($$$1 \le a_i \le 10^4$$$) — the elements of the array $$$a$$$. | 800 | If there's a subsequence of $$$a$$$ whose product isn't a perfect square, print "YES". Otherwise, print "NO". | standard output | |
PASSED | d50fb397258767cc6b7a51fa7f3d4a6f | train_110.jsonl | 1618839300 | Given an array $$$a$$$ of length $$$n$$$, tell us whether it has a non-empty subsequence such that the product of its elements is not a perfect square.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly zero) elements. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import javafx.util.Pair;
public class Main
{
static void sort(int a[])
{
Random ran = new Random();
for (int i = 0; i < a.length; i++) {
int r = ran.nextInt(a.length);
int temp = a[r];
a[r] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
public static void main(String args[])
{
FastScanner input = new FastScanner();
int tc = input.nextInt();
work: while (tc-- > 0) {
int n = input.nextInt();
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = input.nextInt();
}
sort(a);
for (int i = 0; i < n; i++) {
int rot = (int) Math.sqrt(a[i]);
if(rot*rot!=(a[i]))
{
System.out.println("YES");
continue work;
}
}
System.out.println("NO");
}
}
static class FastScanner
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next()
{
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine() throws IOException
{
return br.readLine();
}
}
}
| Java | ["2\n3\n1 5 4\n2\n100 10000"] | 1 second | ["YES\nNO"] | NoteIn the first example, the product of the whole array ($$$20$$$) isn't a perfect square.In the second example, all subsequences have a perfect square product. | Java 8 | standard input | [
"math",
"number theory"
] | 33a31edb75c9b0cf3a49ba97ad677632 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_{n}$$$ ($$$1 \le a_i \le 10^4$$$) — the elements of the array $$$a$$$. | 800 | If there's a subsequence of $$$a$$$ whose product isn't a perfect square, print "YES". Otherwise, print "NO". | standard output | |
PASSED | ba34620e2bf3e24c547c499033b74fb5 | train_110.jsonl | 1618839300 | Given an array $$$a$$$ of length $$$n$$$, tell us whether it has a non-empty subsequence such that the product of its elements is not a perfect square.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly zero) elements. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static boolean isPerfectSquare(int x) {
if (x >= 0) {
int sr = (int)Math.sqrt(x);
return ((sr * sr) == x);
}
return false;
}
public static void main(String[] args) {
try {
System.setIn(new FileInputStream("input.txt"));
System.setOut(new PrintStream(new FileOutputStream("output.txt")));
} catch (Exception e) {
System.err.println("Error");
}
FastReader scn = new FastReader();
int t = scn.nextInt();
while (t-- > 0) {
int n = scn.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scn.nextInt();
}
int flag = 1;
for (int i = 0; i < n; i++) {
int temp = arr[i];
//System.out.println(temp + " " + isPerfectSquare(temp));
if (!isPerfectSquare(temp)) {
flag = 0;
System.out.println("YES");
break;
}
}
if (flag == 1)
System.out.println("NO");
}
}
}
| Java | ["2\n3\n1 5 4\n2\n100 10000"] | 1 second | ["YES\nNO"] | NoteIn the first example, the product of the whole array ($$$20$$$) isn't a perfect square.In the second example, all subsequences have a perfect square product. | Java 8 | standard input | [
"math",
"number theory"
] | 33a31edb75c9b0cf3a49ba97ad677632 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_{n}$$$ ($$$1 \le a_i \le 10^4$$$) — the elements of the array $$$a$$$. | 800 | If there's a subsequence of $$$a$$$ whose product isn't a perfect square, print "YES". Otherwise, print "NO". | standard output | |
PASSED | d3a77abbfc48f59293b0adfa9fc37c4c | train_110.jsonl | 1618839300 | Given an array $$$a$$$ of length $$$n$$$, tell us whether it has a non-empty subsequence such that the product of its elements is not a perfect square.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly zero) elements. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner o=new Scanner(System.in);
int t=o.nextInt();
while(t>0) {
int n=o.nextInt();
int ar[]=new int[n];
for(int i=0;i<n;i++) {
ar[i]=o.nextInt();
}
boolean ps=true;
for(int i=0;i<n;i++) {
int v=(int)Math.sqrt(ar[i]);
if(v*v!=ar[i]) {
ps=false;
break;
}
}
if(!ps)
System.out.println("YES");
else
System.out.println("NO");
t--;
}
}
} | Java | ["2\n3\n1 5 4\n2\n100 10000"] | 1 second | ["YES\nNO"] | NoteIn the first example, the product of the whole array ($$$20$$$) isn't a perfect square.In the second example, all subsequences have a perfect square product. | Java 17 | standard input | [
"math",
"number theory"
] | 33a31edb75c9b0cf3a49ba97ad677632 | The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 100$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$\ldots$$$, $$$a_{n}$$$ ($$$1 \le a_i \le 10^4$$$) — the elements of the array $$$a$$$. | 800 | If there's a subsequence of $$$a$$$ whose product isn't a perfect square, print "YES". Otherwise, print "NO". | standard output | |
PASSED | b3e53a53a1209341fe9f92b476254dc1 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
import java.awt.geom.*;
import static java.lang.Math.*;
public class Solution implements Runnable {
long mod1 = (long) 1e9 + 7;
int mod2 = 998244353;
public void solve() throws Exception {
int n=sc.nextInt();
ArrayList<Integer> a=new ArrayList<>();
a.add(1);
long prod=1;
for(int i=2;i<n;i++) {
if(gcd(i,n)==1 ) {
a.add(i);
prod=(prod*i)%n;
}
}
if(prod!=1) {
for(int i=0;i<a.size();i++) {
if(a.get(i)==prod) {
a.remove(i);
break;
}
}
}
out.println(a.size());
for(int i:a) out.print(i+" ");
}
static boolean isPrime(int n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static long ncr(int n, int r, long p) {
if (r > n)
return 0l;
if (r > n - r)
r = n - r;
long C[] = new long[r + 1];
C[0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = Math.min(i, r); j > 0; j--)
C[j] = (C[j] + C[j - 1]) % p;
}
return C[r] % p;
}
void sieveOfEratosthenes(boolean prime[], int size) {
for (int i = 0; i < size; i++)
prime[i] = true;
for (int p = 2; p * p < size; p++) {
if (prime[p] == true) {
for (int i = p * p; i < size; i += p)
prime[i] = false;
}
}
}
static int LowerBound(int a[], int x) { // smallest index having value >= x; returns 0-based index
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] >= x)
r = m;
else
l = m;
}
return r;
}
static int UpperBound(int a[], int x) {// biggest index having value <= x; returns 1-based index
int l = -1, r = a.length;
while (l + 1 < r) {
int m = (l + r) >>> 1;
if (a[m] <= x)
l = m;
else
r = m;
}
return l + 1;
}
public long power(long x, long y, long p) {
long res = 1;
// out.println(x+" "+y);
x = x % p;
if (x == 0)
return 0;
while (y > 0) {
if ((y & 1) == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static Throwable uncaught;
BufferedReader in;
FastScanner sc;
PrintWriter out;
@Override
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
sc = new FastScanner(in);
solve();
} catch (Throwable uncaught) {
Solution.uncaught = uncaught;
} finally {
out.close();
}
}
public static void main(String[] args) throws Throwable {
Thread thread = new Thread(null, new Solution(), "", (1 << 26));
thread.start();
thread.join();
if (Solution.uncaught != null) {
throw Solution.uncaught;
}
}
}
class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}
public int[] readArray(int n) throws Exception {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}
public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | c4acf17e9a5aac9642a29691294ace35 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | // package eround101;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.util.StringTokenizer;
import java.util.concurrent.TimeUnit;
public class C101 {
static HritikScanner sc = new HritikScanner();
static PrintWriter pw = new PrintWriter(System.out, true);
final static int MOD = (int) 1e9 + 7;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) {
int t = 1;
while (t-- > 0) {
solve();
}
}
static void solve() {
// int n = ni();
// int m = ni();
// int[][] arr = new int[n][m];
// List<Pair> list = new ArrayList<>();
// for(int i = 0; i < n; i++)
// {
// for(int j = 0; j < m; j++)
// {
// arr[i][j] = ni();
// list.add(new Pair(arr[i][j], i, j));
// }
// }
// Collections.sort(list);
//// for(int i = 0; i < list.size(); i++)
//// {
//// pl(list.get(i).num);
//// }
//// pl(list.toString());
// for(int i = 0; i< m; i++)
// {
//// pl("to change int col " +i + " " +arr[list.get(i).row][list.get(i).col]);
// int temp = arr[list.get(i).row][i];
// arr[list.get(i).row][i] = arr[list.get(i).row][list.get(i).col];
// arr[list.get(i).row][list.get(i).col] = temp;
// int rc = list.get(i).row;
// int cc = list.get(i).col;
// Pair p = list.remove(i);
//// pl(list.toString());
// int numb = p.num;
// int row = p.row;
// int col = p.col;
// list.add(i, new Pair(numb, row, i));
//// pl(list.toString());
//// Pair pr = new Pair(arr[list.get(i).row][list.get(i).col], row, list.get(i).col);
// int ind = -1;
//// pl(pr.toString());
// for(int j = 0; j< list.size(); j++)
// {
// if(list.get(j).num == arr[rc][cc])
// {
// ind = j;
// break;
// }
// }
// Pair pr = list.remove(ind);
//// pl(pr.toString());
// int numb1 = pr.num;
//// pl(list.toString());
// list.add(ind, new Pair(numb1, row, col));
//// pl(list.toString());
// }
//// pl(list.toString());
// pa(arr);
int n = ni();
List<Integer> l = new ArrayList<>();
long prod = 1;
for(int i = 1; i< n; i++)
{
if(gcd(n, i) == 1)
{
l.add(i);
prod = (prod*i);
prod = prod %n;
}
}
// pl(prod);
// prod = prod % n;
// pa(l);
if(prod % n != 1)
{
l.remove(l.size()-1);
}
pl(l.size());
pa(l);
}
/////////////////////////////////////////////////////////////////////////////////
static int[] nextIntArray(int n) {
int[] arr = new int[n];
int i = 0;
while (i < n) {
arr[i++] = ni();
}
return arr;
}
static long[] nextLongArray(int n) {
long[] arr = new long[n];
int i = 0;
while (i < n) {
arr[i++] = nl();
}
return arr;
}
static int[] nextIntArray1(int n) {
int[] arr = new int[n + 1];
int i = 1;
while (i <= n) {
arr[i++] = ni();
}
return arr;
}
/////////////////////////////////////////////////////////////////////////////////
static int ni() {
return sc.nextInt();
}
static long nl() {
return sc.nextLong();
}
static double nd() {
return sc.nextDouble();
}
/////////////////////////////////////////////////////////////////////////////////
static void pl() {
pw.println();
}
static void p(Object o) {
pw.print(o+ " ");
}
static void pl(Object o) {
pw.println(o);
}
static void psb(StringBuilder sb) {
pw.print(sb);
}
static void pa(Object arr[]) {
for (Object o : arr) {
p(o);
}
pl();
}
static void pa(int arr[]) {
for (int o : arr) {
p(o);
}
pl();
}
static void pa(long arr[]) {
for (long o : arr) {
p(o);
}
pl();
}
static void pa(double arr[]) {
for (double o : arr) {
p(o);
}
pl();
}
static void pa(char arr[]) {
for (char o : arr) {
p(o);
}
pl();
}
static void pa(List list) {
for (Object o : list) {
p(o);
}
pl();
}
static void pa(Object[][] arr) {
for (int i = 0; i < arr.length; ++i) {
for (Object o : arr[i]) {
p(o);
}
pl();
}
}
static void pa(int[][] arr) {
for (int i = 0; i < arr.length; ++i) {
for (int o : arr[i]) {
p(o);
}
pl();
}
}
static void pa(long[][] arr) {
for (int i = 0; i < arr.length; ++i) {
for (long o : arr[i]) {
p(o);
}
pl();
}
}
static void pa(char[][] arr) {
for (int i = 0; i < arr.length; ++i) {
for (char o : arr[i]) {
p(o);
}
pl();
}
}
static void pa(double[][] arr) {
for (int i = 0; i < arr.length; ++i) {
for (double o : arr[i]) {
p(o);
}
pl();
}
}
/////////////////////////////////////////////////////////////////////////////////
static void print(Object s) {
System.out.println(s);
}
/////////////////////////////////////////////////////////////////////////////////
//-----------HritikScanner class for faster input----------//
static class HritikScanner {
BufferedReader br;
StringTokenizer st;
public HritikScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//////////////////////////////////////////////////////////////////
public static class Pair implements Comparable<Pair> {
int num, row,col;
Pair(int num, int row, int col) {
this.num = num;
this.row = row;
this.col = col;
}
public int get1() {
return num;
}
public int get2() {
return row;
}
public int compareTo(Pair A) {
return this.num - A.num;
}
public String toString()
{
return num +" "+ row+" "+col;
}
}
//////////////////////////////////////////////////////////////////
// Function to return gcd of a and b time complexity O(log(a+b))
static long gcd(long a, long b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
// method to return LCM of two numbers
static long lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
//////////////////////////////////////////////////////////////////
static boolean isPrime(long n) {
// Corner cases
if (n <= 1) {
return false;
}
if (n <= 3) {
return true;
}
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0) {
return false;
}
for (long i = 5; i * i <= n; i = i + 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
//////////////////////////////////////////////////////////////////
static boolean isPowerOfTwo(long n) {
if (n == 0) {
return false;
}
return (long) (Math.ceil((Math.log(n) / Math.log(2))))
== (long) (Math.floor(((Math.log(n) / Math.log(2)))));
}
public static long getFact(int n) {
long ans = 1;
while (n > 0) {
ans *= n;
ans %= MOD;
n--;
}
return ans;
}
public static long pow(long n, int pow) {
if (pow == 0) {
return 1;
}
long temp = pow(n, pow / 2) % MOD;
temp *= temp;
temp %= MOD;
if (pow % 2 == 1) {
temp *= n;
}
temp %= MOD;
return temp;
}
public static long nCr(int n, int r) {
long ans = 1;
int temp = n - r;
while (n > temp) {
ans *= n;
ans %= MOD;
n--;
}
ans *= pow(getFact(r) % MOD, MOD - 2) % MOD;
ans %= MOD;
return ans;
}
//////////////////////////////////////////////////////////////////
// method returns Nth power of A
static double nthRoot(int A, int N) {
// intially guessing a random number between
// 0 and 9
double xPre = Math.random() % 10;
// smaller eps, denotes more accuracy
double eps = 0.001;
// initializing difference between two
// roots by INT_MAX
double delX = 2147483647;
// xK denotes current value of x
double xK = 0.0;
// loop untill we reach desired accuracy
while (delX > eps) {
// calculating current value from previous
// value by newton's method
xK = ((N - 1.0) * xPre
+ (double) A / Math.pow(xPre, N - 1)) / (double) N;
delX = Math.abs(xK - xPre);
xPre = xK;
}
return xK;
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 831ac724af110bfbe817b2e3c8aeb7c1 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer> ans = new ArrayList<>();
ans.add(1);
if( n!= 2)
{
long prod = 1;
for(int i=2; i<n-1; i++)
{
if( GCD(i, n) == 1 )
{
ans.add(i);
prod *= i;
prod = prod%n;
}
}
prod *= n-1;
if( prod % n == 1 )
ans.add(n-1);
}
System.out.println(ans.size());
for(Integer ele : ans )
System.out.print(ele + " ");
}
public static int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | adbf05e877967e3a676fbd9144a8f47b | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.stream.IntStream;
import java.util.Scanner;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author valeesi
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
CProduct1ModuloN solver = new CProduct1ModuloN();
solver.solve(1, in, out);
out.close();
}
static class CProduct1ModuloN {
private static int gcdThing(int a, int b) {
BigInteger b1 = BigInteger.valueOf(a);
BigInteger b2 = BigInteger.valueOf(b);
BigInteger gcd = b1.gcd(b2);
return gcd.intValue();
}
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int total = 0;
int[] arr = IntStream.range(1, n).toArray();
long result = 1;
for (int i = 1; i < n; i++) {
if (gcdThing(i, n) != 1) {
arr[i - 1] = -1;
} else {
result = (result * i) % n;
total++;
}
}
if (result != 1) {
arr[(int) result - 1] = -1;
total--;
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
if (arr[i] != -1) {
sb.append(arr[i] + " ");
}
}
out.println(total);
out.println(sb.toString());
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public Scanner scanner;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
scanner = new Scanner(reader);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 48e88333e4b29f7447c583ba8099933f | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) {
Scanner io = new Scanner(System.in) ;
long n = io.nextLong();
long coprime[] = new long[(int)n];
coprime[1] = 1;
long rem = 1;
long ans = 1;
for(int i=2;i<n;i++) {
if(gcd(i,n) == 1){
coprime[i] = 1;
ans++;
rem = (rem*i)%n;
}
}
if(rem != 1){
ans--;
coprime[(int)rem] = 0;
}
System.out.println(ans);
for(int i=1;i<n;i++){
if(coprime[i]==1)System.out.print(i + " ");
}
io.close(); // make sure to include this line -- closes io and flushes the output
}
private static long gcd(long a, long b) {
if(b == 0){
return a;
}
return gcd(b, a%b);
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | a5c23939ba5ad8ba5c6949f3e8693696 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.util.Set;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Manav
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
CProduct1ModuloN solver = new CProduct1ModuloN();
solver.solve(1, in, out);
out.close();
}
static class CProduct1ModuloN {
public void solve(int testNumber, InputReader in, OutputWriter out) {
long n = in.nextLong();
Set<Long> set = new LinkedHashSet<Long>();
long product = 1;
for (int i = 1; i < n; i++) {
if (gcd(i, n) == 1) {
set.add((long) i);
product = (product * (i)) % n;
}
}
// out.println(product);
if (product != 1) {
set.remove(product);
}
out.println(set.size());
for (long x : set) {
out.print(x + " ");
}
out.println();
}
private long gcd(long a, long b) {
while (b > 0) {
long temp = a % b;
a = b;
b = temp;
}
return a;
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println() {
writer.println();
}
public void close() {
writer.close();
}
public void println(int i) {
writer.println(i);
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | d5a3350fd0c58bb81de94f4aad4a2f0c | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(System.out);
int n=Integer.parseInt(br.readLine());
ArrayList<Integer> arr=new ArrayList<>();
//arr.add(1);
//long sum=1;
for(int i=1;i<=n-2;i++)
{
if(gcd(i,n)==1){
arr.add(i);
// sum=(sum*(long)i)%n;
}
}
long sum=1;
for(int i=0;i<arr.size();i++)
{
sum*=(long)arr.get(i);
sum%=n;
}
if((sum*(long)(n-1))%n==1)
arr.add(n-1);
System.out.println(arr.size());
for(int i=0;i<arr.size();i++)
out.print(arr.get(i)+" ");
out.print("\n");
out.flush();
}
public static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 3d600399bafe467c52dc73b3f368d6f4 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;
public class Main {
private static final String NO = "NO";
private static final String YES = "YES";
InputStream is;
PrintWriter out;
String INPUT = "";
private static final long MOD = 1000000007;
private static final int MAXN = 100000;
void solve() {
int T = 1;//ni();
for (int i = 0; i < T; i++) {
solve(i);
}
}
void solve(int T) {
int n = ni();
boolean b[] = new boolean[n];
long ans = 1;
int cnt = 0;
for (int i = 1; i < n; i++) {
if (gcd(i, n) == 1) {
b[i] = true;
ans = (ans * i) % n;
cnt++;
}
}
if (ans != 1) {
b[(int) ans] = false;
cnt--;
}
out.println(cnt);
for (int i = 1; i < n; i++) {
if (b[i])
out.print(i + " ");
}
out.println();
}
// a^b
long power(long a, long b) {
long x = 1, y = a;
while (b > 0) {
if (b % 2 != 0) {
x = (x * y) % MOD;
}
y = (y * y) % MOD;
b /= 2;
}
return x % MOD;
}
private long gcd(long a, long b) {
while (a != 0) {
long tmp = b % a;
b = a;
a = tmp;
}
return b;
}
void run() throws Exception {
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
if (!INPUT.isEmpty())
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new Main().run();
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
// ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n) {
if (!(isSpaceChar(b)))
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private List<Integer> na2(int n) {
List<Integer> a = new ArrayList<Integer>();
for (int i = 0; i < n; i++)
a.add(ni());
return a;
}
private int[][] na(int n, int m) {
int[][] a = new int[n][];
for (int i = 0; i < n; i++)
a[i] = na(m);
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
private long[][] nl(int n, int m) {
long[][] a = new long[n][];
for (int i = 0; i < n; i++)
a[i] = nl(m);
return a;
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) {
System.out.println(Arrays.deepToString(o));
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 6185b4b5016b975dd0ce4df54ffe04b5 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
public class Solution {
static long mod=(long)(1e9+7);
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// int t = sc.nextInt();
// while(t-->0){
long n=sc.nextInt();
// long k=sc.nextLong();
List<Long> ans=new ArrayList<>();
long i=1l;
long prod=1l;
while(i<n){
if(gcd(i,n)==1l){
ans.add(i);
prod=(prod*i)%n;
}
i++;
}
// System.out.println(prod);
// for(int k:ans)
// System.out.print(k+" ");
long x=-1l;
if(prod!=1l ){
x=prod;
}
if(x==-1l)
System.out.println(ans.size());
else
System.out.println(ans.size()-1);
for(long k:ans)
{
if(k!=x)
System.out.print(k+" ");
}
System.out.println("");
}
// }
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | abdf2b001cb048d20a8910d89cd17bfb | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes |
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.Random;
import java.util.*;
public class ProductModuloN implements Runnable {
int[] arr = new int[100005];
void solve() throws IOException {
int n = ri();
long prod = 1;
for (int num = 1; num < n; num++) {
if (gcd(n, num) == 1) {
arr[num] = 1;
prod = (prod * num) % n;
}
}
if (prod != 1)
arr[(int) prod] = 0;
println(count(arr, n));
for (int i = 1; i < n; i++) {
if (arr[i] == 1) {
sbr.append(i).append(" ");
}
}
print(sbr.toString());
}
int count(int[] arr, int n) {
int count = 0;
for (int i = 1; i < n; i++)
if (arr[i] == 1)
count++;
return count;
}
long gcd(long a, long b) {
return (b > 0) ? gcd(b, a % b) : a;
}
/************************************************************************************************************************************************/
public static void main(String[] args) throws IOException {
new Thread(null, new ProductModuloN(), "1").start();
}
static PrintWriter out = new PrintWriter(System.out);
static Reader read = new Reader();
static StringBuilder sbr = new StringBuilder();
static int mod = (int) 1e9 + 7;
static int dmax = Integer.MAX_VALUE;
static long lmax = Long.MAX_VALUE;
static int dmin = Integer.MIN_VALUE;
static long lmin = Long.MIN_VALUE;
@Override
public void run() {
try {
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
static class Reader {
private byte[] buf = new byte[1024];
private int index;
private InputStream in;
private int total;
public Reader() {
in = System.in;
}
public int scan() throws IOException {
if (total < 0)
throw new InputMismatchException();
if (index >= total) {
index = 0;
total = in.read(buf);
if (total <= 0)
return -1;
}
return buf[index++];
}
public int intNext() throws IOException {
int integer = 0;
int n = scan();
while (isWhiteSpace(n))
n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
} else
throw new InputMismatchException();
}
return neg * integer;
}
public double doubleNext() throws IOException {
double doub = 0;
int n = scan();
while (isWhiteSpace(n))
n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n) && n != '.') {
if (n >= '0' && n <= '9') {
doub *= 10;
doub += n - '0';
n = scan();
} else
throw new InputMismatchException();
}
if (n == '.') {
n = scan();
double temp = 1;
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
temp /= 10;
doub += (n - '0') * temp;
n = scan();
} else
throw new InputMismatchException();
}
}
return doub * neg;
}
public String read() throws IOException {
StringBuilder sb = new StringBuilder();
int n = scan();
while (isWhiteSpace(n))
n = scan();
while (!isWhiteSpace(n)) {
sb.append((char) n);
n = scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1)
return true;
return false;
}
}
static void shuffle(int[] aa) {
int n = aa.length;
Random rand = new Random();
for (int i = 1; i < n; i++) {
int j = rand.nextInt(i + 1);
int tmp = aa[i];
aa[i] = aa[j];
aa[j] = tmp;
}
}
static void shuffle(int[][] aa) {
int n = aa.length;
Random rand = new Random();
for (int i = 1; i < n; i++) {
int j = rand.nextInt(i + 1);
int first = aa[i][0];
int second = aa[i][1];
aa[i][0] = aa[j][0];
aa[i][1] = aa[j][1];
aa[j][0] = first;
aa[j][1] = second;
}
}
static void print(Object object) {
out.print(object);
}
static void println(Object object) {
out.println(object);
}
static int[] iArr(int len) {
return new int[len];
}
static long[] lArr(int len) {
return new long[len];
}
static long min(long a, long b) {
return Math.min(a, b);
}
static int min(int a, int b) {
return Math.min(a, b);
}
static long max(Long a, Long b) {
return Math.max(a, b);
}
static int max(int a, int b) {
return Math.max(a, b);
}
static int ri() throws IOException {
return read.intNext();
}
static long rl() throws IOException {
return Long.parseLong(read.read());
}
static String rs() throws IOException {
return read.read();
}
static char rc() throws IOException {
return rs().charAt(0);
}
static double rd() throws IOException {
return read.doubleNext();
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 83834610b4a9a98abd996c789f7392a4 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class GFG {
public static int gcd(int n,int m)
{
if(m==0)
return n;
else{
return gcd(m,n%m);
}
}
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int mod=1000000007;
ArrayList<Integer> al =new ArrayList<>();
long j=1;
for(int i=1;i<n;i++ )
{
if(gcd(i,n)==1)
{
j=(j*i)%n;
al.add(i);
}
}
int l=al.size();
if(j!=1)
{
al.remove(l-1);
l=l-1;
}
System.out.println(l);
for(int i=0;i<l;i++)
{
System.out.print(al.get(i)+" ");
}
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | c57541c53549f5bb07fdac6304cc7ea8 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
import static java.lang.System.in;
import static java.lang.System.out;
public class Main{
final static int INF=Integer.MAX_VALUE;
final static int nINF=Integer.MIN_VALUE;
final static long MOD=1000000007;
public static void main(String[] args){
FastReader fr=new FastReader();
int tc=1;
while(tc-->0){
int n=fr.nextInt();
ArrayList<Integer>al=new ArrayList<>();
long mult=1;
for (int i=1;i<=n;i++){
if(gcd(i,n)==1){
al.add(i);
mult=(mult*i)%n;
}
}
int tr=-1;
if(mult>1){
tr=(int)mult;
}
if(tr==-1){
out.println(al.size());
}
else {
out.println(al.size()-1);
}
StringBuilder sb=new StringBuilder();
for (int here:al){
if (here!=tr){
sb.append(here+" ");
}
}
sb.deleteCharAt(sb.length()-1);
out.println(sb);
}
}
///////////////////////////////////////////////////////////
static int[] fillInt(int n,FastReader fr){
int ret[]=new int[n];
for(int i=0;i<n;i++){
ret[i]=fr.nextInt();
}
return ret;
}
static long[] fillLong(int n,FastReader fr){
long ret[]=new long[n];
for(int i=0;i<n;i++){
ret[i]=fr.nextInt();
}
return ret;
}
static int max(int a,int b){
return Math.max(a,b);
}
static long max(long a,long b){
return Math.max(a,b);
}
static int min(int a,int b){
return Math.min(a,b);
}
static long min(long a,long b){
return Math.min(a,b);
}
static int modDivide(int a,int b){
return (int) (a*Math.pow(b,MOD-2)%MOD);
}
static int gcd(int a,int b){
if(b==0){
return a;
}
return gcd(b,a%b);
}
static long gcd(long a,long b){
if(b==0){
return a;
}
return gcd(b,a%b);
}
static int lcm(int a,int b){
return (a*b)/gcd(a,b);
}
static int longestProperPrefixSuffix(String str){
// KMP
int n=str.length();
int st[]=new int[n];
int len=0,c=1;
while(c<n){
if(str.charAt(len)==str.charAt(c)){
len++;
st[c]=len;
c++;
}else{
if(len>0){
len=st[len-1];
}else{
st[c]=0;
c++;
}
}
}
// out.println(Arrays.toString(st));
return st[n-1];
}
static int lowerBound(int a[],int val){
int ret=Arrays.binarySearch(a,val);
if(ret<0){
return -1;
}else{
while(ret>0 && a[ret-1]==val){
ret--;
}
return ret;
}
}
static int upperBound(int a[],int val){
int ret=Arrays.binarySearch(a,val);
if(ret<0){
return -1;
}else{
while(ret<a.length-1 && a[ret+1]==val){
ret++;
}
return ret;
}
}
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
byte nextByte(){
return Byte.parseByte(next());
}
Float nextFloat(){
return Float.parseFloat(next());
}
long nextLong(){
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
short nextShort(){
return Short.parseShort(next());
}
Boolean nextbol(){
return Boolean.parseBoolean(next());
}
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | c49d2c7f28a71c7acb8cb48407686f1a | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class A {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
// boolean[] is = sieve(100000);
int n = sc.nextInt();
List<Integer> list = new ArrayList<>();
for(int i = 1; i < n; i++) {
if(gcd(i,n) == 1) {
list.add(i);
}
}
long mul = 1L;
for(int e : list) {
mul *= e*1l;
mul %= n;
}
int index = -1;
for(int i = list.size()-1 ; i >= 0; i--) {
// System.out.println(mul + " " + (is(mul,n)));
if(is(mul,n)) {
index = i;
break;
}
mul /= list.get(i);
}
if(index == -1) {
out.println("1");
out.println("1");
out.close();
return;
}
out.println(index+1);
for(int i = 0; i <= index; i++) {
out.print(list.get(i) + " ");
}
out.println();
out.close();
}
private static boolean is(long a, int n) {
int div = (int)(a/(n*1l));
if((n*1L*div+1)*1l == a) return true;
return false;
}
static class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
static void ruffleSort(int[] a) {
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n), temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static boolean[] sieve(int n) {
boolean isPrime[] = new boolean[n + 1];
for (int i = 2; i <= n; i++) {
if (isPrime[i])
continue;
for (int j = 2 * i; j <= n; j += i) {
isPrime[j] = true;
}
}
return isPrime;
}
static int mod = 1000000007;
static long pow(int a, long b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
if (b % 2 == 0) {
long ans = pow(a, b / 2);
return ans * ans;
} else {
long ans = pow(a, (b - 1) / 2);
return a * ans * ans;
}
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
// For Input.txt and Output.txt
// FileInputStream in = new FileInputStream("input.txt");
// FileOutputStream out = new FileOutputStream("output.txt");
// PrintWriter pw = new PrintWriter(out);
// Scanner sc = new Scanner(in);
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 61706d9cf07892e6cf5da3c5ba3da769 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | // have faith in yourself!!
/*
Naive mistakes in java :
--> Arrays.sort(primitive) is O(n^2)
--> Never use '=' to compare to Integer data types, instead use 'equals()'
--> -4 % 3 = -1, actually it should be 2, so add '+n' to modulo result
Stuffs to do when you have no idea how to proceed
--> Maybe try brute force and find patter/observations
*/
import java.io.*;
import java.util.*;
public class CodeForces {
public static void main(String[] args) throws IOException {
openIO();
int testCase = 1;
// testCase = sc. nextInt();
for (int i = 1; i <= testCase; i++) solve(i);
closeIO();
}
/*-------------------------------------------EDITING CODE STARTS HERE-------------------------------------------*/
public static void solve(int tCase) throws IOException {
int n = sc.nextInt();
List<Integer> ans = new ArrayList<>();
long prod = 1;
for(int i=1;i<=n-1;i++) {
if (_gcd(i, n) == 1){
ans.add(i); prod *= i;
prod %=n;
}
}
int j = ans.size()-1;
if(prod !=1)j--;
out.println(j+1);
for(int i=0;i<=j;i++)out.println(ans.get(i));
}
/*-------------------------------------------EDITING CODE ENDS HERE-------------------------------------------*/
public static int mod = (int) 1e9 + 7;
// public static int mod = 998244353;
public static int inf_int = (int) 2e9;
public static long inf_long = (long) 2e18;
public static void _sort(int[] arr, boolean isAscending) {
int n = arr.length;
List<Integer> list = new ArrayList<>();
for (int ele : arr) list.add(ele);
Collections.sort(list);
if (!isAscending) Collections.reverse(list);
for (int i = 0; i < n; i++) arr[i] = list.get(i);
}
public static void _sort(long[] arr, boolean isAscending) {
int n = arr.length;
List<Long> list = new ArrayList<>();
for (long ele : arr) list.add(ele);
Collections.sort(list);
if (!isAscending) Collections.reverse(list);
for (int i = 0; i < n; i++) arr[i] = list.get(i);
}
// time : O(1), space : O(1)
public static int _digitCount(long num,int base){
// this will give the # of digits needed for a number num in format : base
return (int)(1 + Math.log(num)/Math.log(base));
}
// time : O(n), space: O(n)
public static long _fact(int n){
// simple factorial calculator
long ans = 1;
for(int i=2;i<=n;i++)
ans = ans * i % mod;
return ans;
}
// time for pre-computation of factorial and inverse-factorial table : O(nlog(mod))
public static long[] factorial , inverseFact;
public static void _ncr_precompute(int n){
factorial = new long[n+1];
inverseFact = new long[n+1];
factorial[0] = inverseFact[0] = 1;
for (int i = 1; i <=n; i++) {
factorial[i] = (factorial[i - 1] * i) % mod;
inverseFact[i] = _modExpo(factorial[i], mod - 2);
}
}
// time of factorial calculation after pre-computation is O(1)
public static int _ncr(int n,int r){
if(r > n)return 0;
return (int)(factorial[n] * inverseFact[r] % mod * inverseFact[n - r] % mod);
}
public static int _npr(int n,int r){
if(r > n)return 0;
return (int)(factorial[n] * inverseFact[n - r] % mod);
}
// euclidean algorithm time O(max (loga ,logb))
public static long _gcd(long a, long b) {
while (a>0){
long x = a;
a = b % a;
b = x;
}
return b;
// if (a == 0)
// return b;
// return _gcd(b % a, a);
}
// lcm(a,b) * gcd(a,b) = a * b
public static long _lcm(long a, long b) {
return (a / _gcd(a, b)) * b;
}
// binary exponentiation time O(logn)
public static long _modExpo(long x, long n) {
long ans = 1;
while (n > 0) {
if ((n & 1) == 1) {
ans *= x;
ans %= mod;
n--;
} else {
x *= x;
x %= mod;
n >>= 1;
}
}
return ans;
}
public static long _modInv(long a,long b){
return (a * _modExpo(b,mod-2)) % mod;
}
//sieve or first divisor time : O(mx * log ( log (mx) ) )
public static int[] _seive(int mx){
int[] firstDivisor = new int[mx+1];
for(int i=0;i<=mx;i++)firstDivisor[i] = i;
for(int i=2;i*i<=mx;i++)
if(firstDivisor[i] == i)
for(int j = i*i;j<=mx;j+=i)
firstDivisor[j] = i;
return firstDivisor;
}
// check if x is a prime # of not. time : O( n ^ 1/2 )
private static boolean _isPrime(long x){
for(long i=2;i*i<=x;i++)
if(x%i==0)return false;
return true;
}
static class Pair<K, V>{
K ff;
V ss;
public Pair(K ff, V ss) {
this.ff = ff;
this.ss = ss;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || this.getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return ff.equals(pair.ff) && ss.equals(pair.ss);
}
@Override
public int hashCode() {
return Objects.hash(ff, ss);
}
@Override
public String toString(){
return ff.toString()+" "+ss.toString();
}
}
static FastestReader sc;
static PrintWriter out;
private static void openIO() throws IOException {
sc = new FastestReader();
out = new PrintWriter(System.out);
}
/*-------------------------------------------FAST INPUT STARTS HERE---------------------------------------------*/
public static void closeIO() throws IOException {
out.flush();
out.close();
sc.close();
}
private static final class FastestReader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
public FastestReader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public FastestReader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() throws IOException {
int b;
//noinspection StatementWithEmptyBody
while ((b = read()) != -1 && isSpaceChar(b)) {
}
return b;
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) {
return -ret;
}
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) {
buffer[0] = -1;
}
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
public void close() throws IOException {
din.close();
}
}
/*---------------------------------------------FAST INPUT ENDS HERE ---------------------------------------------*/
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 4dc2a69fe1430493fdbf7a80158342d3 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | // have faith in yourself!!
/*
Naive mistakes in java :
--> Arrays.sort(primitive) is O(n^2)
--> Never use '=' to compare to Integer data types, instead use 'equals()'
--> -4 % 3 = -1, actually it should be 2, so add '+n' to modulo result
*/
import java.io.*;
import java.util.*;
public class CodeForces {
public static void main(String[] args) throws IOException {
openIO();
int testCase = 1;
// testCase = sc. nextInt();
for (int i = 1; i <= testCase; i++) solve(i);
closeIO();
}
/*-------------------------------------------EDITING CODE STARTS HERE-------------------------------------------*/
public static void solve(int tCase) throws IOException {
int n = sc.nextInt();
List<Integer> ans = new ArrayList<>();
long prod = 1;
for(int i=1;i<=n-1;i++) {
if (_gcd(i, n) == 1){
ans.add(i); prod *= i;
prod %=n;
}
}
int j = ans.size()-1;
while (prod %n !=1){
prod /= ans.get(j--);
}
out.println(j+1);
for(int i=0;i<=j;i++)out.println(ans.get(i));
// memo(1,1,n,"");
// out.println(ss);
}
private static String ss = "";
private static void memo(int i,long curr,int n,String str){
if(i==n){
if(curr % n==1)if(str.length() > ss.length())ss = str;
return;
}
memo(i+1,curr,n,str);
memo(i+1,curr*i % n,n,str+", "+i);
}
/*-------------------------------------------EDITING CODE ENDS HERE-------------------------------------------*/
public static int mod = (int) 1e9 + 7;
// public static int mod = 998244353;
public static int inf_int = (int) 2e9;
public static long inf_long = (long) 2e18;
public static void _sort(int[] arr, boolean isAscending) {
int n = arr.length;
List<Integer> list = new ArrayList<>();
for (int ele : arr) list.add(ele);
Collections.sort(list);
if (!isAscending) Collections.reverse(list);
for (int i = 0; i < n; i++) arr[i] = list.get(i);
}
public static void _sort(long[] arr, boolean isAscending) {
int n = arr.length;
List<Long> list = new ArrayList<>();
for (long ele : arr) list.add(ele);
Collections.sort(list);
if (!isAscending) Collections.reverse(list);
for (int i = 0; i < n; i++) arr[i] = list.get(i);
}
// time : O(1), space : O(1)
public static int _digitCount(long num,int base){
// this will give the # of digits needed for a number num in format : base
return (int)(1 + Math.log(num)/Math.log(base));
}
// time : O(n), space: O(n)
public static long _fact(int n){
// simple factorial calculator
long ans = 1;
for(int i=2;i<=n;i++)
ans = ans * i % mod;
return ans;
}
// time for pre-computation of factorial and inverse-factorial table : O(nlog(mod))
public static long[] factorial , inverseFact;
public static void _ncr_precompute(int n){
factorial = new long[n+1];
inverseFact = new long[n+1];
factorial[0] = inverseFact[0] = 1;
for (int i = 1; i <=n; i++) {
factorial[i] = (factorial[i - 1] * i) % mod;
inverseFact[i] = _modExpo(factorial[i], mod - 2);
}
}
// time of factorial calculation after pre-computation is O(1)
public static int _ncr(int n,int r){
if(r > n)return 0;
return (int)(factorial[n] * inverseFact[r] % mod * inverseFact[n - r] % mod);
}
public static int _npr(int n,int r){
if(r > n)return 0;
return (int)(factorial[n] * inverseFact[n - r] % mod);
}
// euclidean algorithm time O(max (loga ,logb))
public static long _gcd(long a, long b) {
while (a>0){
long x = a;
a = b % a;
b = x;
}
return b;
// if (a == 0)
// return b;
// return _gcd(b % a, a);
}
// lcm(a,b) * gcd(a,b) = a * b
public static long _lcm(long a, long b) {
return (a / _gcd(a, b)) * b;
}
// binary exponentiation time O(logn)
public static long _modExpo(long x, long n) {
long ans = 1;
while (n > 0) {
if ((n & 1) == 1) {
ans *= x;
ans %= mod;
n--;
} else {
x *= x;
x %= mod;
n >>= 1;
}
}
return ans;
}
public static long _modInv(long a,long b){
return (a * _modExpo(b,mod-2)) % mod;
}
//sieve or first divisor time : O(mx * log ( log (mx) ) )
public static int[] _seive(int mx){
int[] firstDivisor = new int[mx+1];
for(int i=0;i<=mx;i++)firstDivisor[i] = i;
for(int i=2;i*i<=mx;i++)
if(firstDivisor[i] == i)
for(int j = i*i;j<=mx;j+=i)
firstDivisor[j] = i;
return firstDivisor;
}
// check if x is a prime # of not. time : O( n ^ 1/2 )
private static boolean _isPrime(long x){
for(long i=2;i*i<=x;i++)
if(x%i==0)return false;
return true;
}
static class Pair<K, V>{
K ff;
V ss;
public Pair(K ff, V ss) {
this.ff = ff;
this.ss = ss;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || this.getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return ff.equals(pair.ff) && ss.equals(pair.ss);
}
@Override
public int hashCode() {
return Objects.hash(ff, ss);
}
@Override
public String toString(){
return ff.toString()+" "+ss.toString();
}
}
static FastestReader sc;
static PrintWriter out;
private static void openIO() throws IOException {
sc = new FastestReader();
out = new PrintWriter(System.out);
}
/*-------------------------------------------FAST INPUT STARTS HERE---------------------------------------------*/
public static void closeIO() throws IOException {
out.flush();
out.close();
sc.close();
}
private static final class FastestReader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
public FastestReader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public FastestReader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() throws IOException {
int b;
//noinspection StatementWithEmptyBody
while ((b = read()) != -1 && isSpaceChar(b)) {
}
return b;
}
public String next() throws IOException {
int b = skip();
final StringBuilder sb = new StringBuilder();
while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg) {
return -ret;
}
return ret;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ') {
c = read();
}
final boolean neg = c == '-';
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) {
return -ret;
}
return ret;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) {
buffer[0] = -1;
}
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
public void close() throws IOException {
din.close();
}
}
/*---------------------------------------------FAST INPUT ENDS HERE ---------------------------------------------*/
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | ce0ee64149c0b7899572e6843cf86bb1 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class cp23 {
static BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
static int mod = 1000000007;
static String toReturn = "";
static int steps = Integer.MAX_VALUE;
static int maxlen = 1000005;
/*MATHEMATICS FUNCTIONS START HERE
MATHS
MATHS
MATHS
MATHS*/
static long gcd(long a, long b) {
if(b == 0) return a;
else return gcd(b, a % b);
}
static long powerMod(long x, long y, int mod) {
if(y == 0) return 1;
long temp = powerMod(x, y / 2, mod);
temp = ((temp % mod) * (temp % mod)) % mod;
if(y % 2 == 0) return temp;
else return ((x % mod) * (temp % mod)) % mod;
}
static long modInverse(long n, int p) {
return powerMod(n, p - 2, p);
}
static long nCr(int n, int r, int mod, long [] fact, long [] ifact) {
return ((fact[n] % mod) * ((ifact[r] * ifact[n - r]) % mod)) % mod;
}
static boolean isPrime(long a) {
if(a == 1) return false;
else if(a == 2 || a == 3 || a== 5) return true;
else if(a % 2 == 0 || a % 3 == 0) return false;
for(int i = 5; i * i <= a; i = i + 6) {
if(a % i == 0 || a % (i + 2) == 0) return false;
}
return true;
}
static int [] seive(int a) {
int [] toReturn = new int [a + 1];
for(int i = 0; i < a; i++) toReturn[i] = 1;
toReturn[0] = 0;
toReturn[1] = 0;
toReturn[2] = 1;
for(int i = 2; i * i <= a; i++) {
if(toReturn[i] == 0) continue;
for(int j = 2 * i; j <= a; j += i) toReturn[j] = 0;
}
return toReturn;
}
/*MATHS
MATHS
MATHS
MATHS
MATHEMATICS FUNCTIONS END HERE */
/*SWAP FUNCTION START HERE
SWAP
SWAP
SWAP
SWAP
*/
static void swap(int i, int j, int [] arr) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/*SWAP
SWAP
SWAP
SWAP
SWAP FUNCTION END HERE*/
/*BINARY SEARCH METHODS START HERE
* BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
*/
static boolean BinaryCheck(long test, long [] arr, long health) {
for(int i = 0; i <= arr.length - 1; i++) {
if(i == arr.length - 1) health -= test;
else if(arr[i + 1] - arr[i] > test) {
health = health - test;
}else {
health = health - (arr[i + 1] - arr[i]);
}
if(health <= 0) return true;
}
return false;
}
static long binarySearchModified(long n, long [] arr) {
long start = 0, end = n, ans = n;
while(start < end) {
long mid = (start + end) / 2;
if(BinaryCheck(mid, arr, n)) {
ans = Math.min(ans, mid);
end = mid;
}else {
start = mid + 1;
}
}
return ans;
}
/*BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
* BINARY SEARCH
BINARY SEARCH METHODS END HERE*/
/*RECURSIVE FUNCTION START HERE
* RECURSIVE
* RECURSIVE
* RECURSIVE
* RECURSIVE
*/
static int recurse(int x, int y, int n, int steps1, Integer [][] dp) {
if(x > n || y > n) return 0;
if(dp[x][y] != null) {
return dp[x][y];
}
else if(x == n || y == n) {
return steps1;
}
return dp[x][y] = Math.max(recurse(x + y, y, n, steps1 + 1, dp), recurse(x, x + y, n, steps1 + 1, dp));
}
/*RECURSIVE
* RECURSIVE
* RECURSIVE
* RECURSIVE
* RECURSIVE
RECURSIVE FUNCTION END HERE*/
/*GRAPH FUNCTIONS START HERE
* GRAPH
* GRAPH
* GRAPH
* GRAPH
* */
static class edge{
int from, to, weight;
public edge(int x, int y, int z) {
this.from = x;
this.to = y;
this.weight = z;
}
}
static void addEdge(ArrayList<ArrayList<edge>> graph, int from, int to, int weight) {
edge temp = new edge(from, to, weight);
edge temp1 = new edge(to, from, weight);
graph.get(from).add(temp);
graph.get(to).add(temp1);
}
static int ans = 0;
static void dfs(ArrayList<ArrayList<edge>> graph, int vertex, boolean [] visited, int [] toReturn, int weight) {
//System.out.println(graph.get(vertex).size());
if(visited[vertex]) return;
visited[vertex] = true;
if(graph.get(vertex).size() > 2) return;
for(int i = 0; i < graph.get(vertex).size(); i++) {
edge temp = graph.get(vertex).get(i);
if(!visited[temp.to]) {
//System.out.println(temp.to);
toReturn[temp.weight] = weight;
dfs(graph, temp.to, visited, toReturn, 5 - weight);
weight = 5 - weight;
}
}
}
static void bfs(ArrayList<ArrayList<edge>> graph, int vertex, boolean [] visited, int [] toReturn, Queue<Integer> q, int weight) {
if(visited[vertex]) return;
visited[vertex] = true;
if(graph.get(vertex).size() > 2) return;
int first = weight;
for(int i = 0; i < graph.get(vertex).size(); i++) {
edge temp = graph.get(vertex).get(i);
if(!visited[temp.to]) {
q.add(temp.to);
toReturn[temp.weight] = weight;
weight = 5 - weight;
}
}
if(!q.isEmpty())bfs(graph, q.poll(), visited, toReturn, q, 5 - first);
}
static void topoSort(ArrayList<ArrayList<Integer>> graph, int vertex, boolean [] visited, ArrayList<Integer> toReturn) {
if(visited[vertex]) return;
visited[vertex] = true;
for(int i = 0; i < graph.get(vertex).size(); i++) {
if(!visited[graph.get(vertex).get(i)]) topoSort(graph, graph.get(vertex).get(i), visited, toReturn);
}
toReturn.add(vertex);
}
static boolean isCyclicDirected(ArrayList<ArrayList<Integer>> graph, int vertex, boolean [] visited, boolean [] reStack) {
if(reStack[vertex]) return true;
if(visited[vertex]) return false;
reStack[vertex] = true;
visited[vertex] = true;
for(int i = 0; i < graph.get(vertex).size(); i++) {
if(isCyclicDirected(graph, graph.get(vertex).get(i), visited, reStack)) return true;
}
reStack[vertex] = false;
return false;
}
/*GRAPH FUNCTIONS END HERE
* GRAPH
* GRAPH
* GRAPH
* GRAPH
*/
/*disjoint Set START HERE
* disjoint Set
* disjoint Set
* disjoint Set
* disjoint Set
*/
static int [] rank;
static int [] parent;
static int parent(int [] parent, int x) {
if(parent[x] == x) return x;
else return parent[x] = parent(parent, parent[x]);
}
static void union(int x, int y, int [] rank, int [] parent) {
if(parent(parent, x) == parent(parent, y)) {
return;
}
if(rank[x] > rank[y]) {
swap(x, y, rank);
}
rank[x] += rank[y];
parent[x] = y;
}
/*disjoint Set END HERE
* disjoint Set
* disjoint Set
* disjoint Set
* disjoint Set
*/
/*INPUT START HERE
* INPUT
* INPUT
* INPUT
* INPUT
* INPUT
*/
static int nexInt() throws NumberFormatException, IOException {
return Integer.parseInt(sc.readLine());
}
static long nexLong() throws NumberFormatException, IOException {
return Long.parseLong(sc.readLine());
}
static long [] inputLongArr() throws NumberFormatException, IOException{
String [] s = sc.readLine().split(" ");
long [] toReturn = new long[s.length];
for(int i = 0; i < s.length; i++) {
toReturn[i] = Long.parseLong(s[i]);
}
return toReturn;
}
static int [] inputIntArr() throws NumberFormatException, IOException{
String [] s = sc.readLine().split(" ");
int [] toReturn = new int[s.length];
for(int i = 0; i < s.length; i++) {
toReturn[i] = Integer.parseInt(s[i]);
}
return toReturn;
}
/*INPUT
* INPUT
* INPUT
* INPUT
* INPUT
* INPUT END HERE
*/
static void solve() throws IOException {
int n = nexInt();
ArrayList<Integer> toReturn = new ArrayList<Integer>();
for(int i = 1; i <= n; i++) {
if(gcd((long)i, (long)n) == 1) toReturn.add(i);
}
long product = 1;
for(int i = 0; i < toReturn.size(); i++) {
product = (product * toReturn.get(i)) % n;
}
if(product % n != 1) {
System.out.println(toReturn.size() - 1);
for(int i = 0; i < toReturn.size() - 1; i++) System.out.print(toReturn.get(i) + " ");
}else {
System.out.println(toReturn.size());
for(int i = 0; i < toReturn.size(); i++) System.out.print(toReturn.get(i) + " ");
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//int t = Integer.parseInt(sc.readLine()); for(int i = 0; i < t; i++)
solve();
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 276f18359f945c382ec17d4fe7a63e71 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
public class Main
{
static long gcd(long a, long b){
if(b == 0) return a;
return gcd(b, a % b);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
ArrayList<Integer> arr = new ArrayList<>();
long product = 1;
for(int i = 1; i < n; i++){
if(gcd(n, i) == 1) {
arr.add(i);
product *= i;
product %= n;
}
}
int sub = 0;
//System.out.println(product);
if(product != 1) sub = 1;
System.out.println(arr.size() - sub);
for(int i = 0; i < arr.size() - sub; i++) System.out.print(arr.get(i) + " ");
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 73783aa965b25c7ba8b8c0ea3f5d6c3b | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class product1 {
public static void main(String[] args) {
// System.out.println(gcd(111,221));
//Scanner sc = new Scanner(System.in);
FastReader sc = new FastReader();
int n = sc.nextInt();
int [] arr = new int[n];
for(int i = 0 ; i<n;i++) {
arr[i] = i+1;
}
ArrayList<Integer> al = new ArrayList<>();
al.add(1);
for(int i = 1 ; i<n;i++) {
if(gcd(arr[i],n)==1) {
al.add(arr[i]);
}
}
int m = al.size();
long ans = 1;
for(int i = 1 ; i<m;i++) {
ans = ans*al.get(i) % n;
}
//System.out.println(ans);
if(ans == 1) {
System.out.println(m);
for(int i = 0 ; i<m;i++) {
System.out.print(al.get(i) + " ");
}
System.out.println();
}else {
// int x = Collections.binarySearch(al, ans);
al.remove(m-1);
System.out.println(m-1);
for(int i = 0 ; i<m-1;i++) {
System.out.print(al.get(i)+ " ");
}
System.out.println();
}
}
public static int gcd(int a , int b) {
if(a==0) {
return b;
}
return gcd(b%a,a);
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | f308eb3012930605439644f7fbbc78ac | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* multiplication of all co-prime numbers of n is also co-prime.
*
*/
public class Main {
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch(IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
long nextLong() {
return Long.parseLong(next());
}
}
public static void main(String[] args) {
FastReader fs = new FastReader();
long n = fs.nextLong();
//co prime numbers
//and then modulo n
//if ans 1 good, else if ans p then remove p
List<Long> list = new ArrayList<>();
long i = 1;
long product = 1;
while(i < n) {
if(gcd(n,i)==1) {
list.add(i);
product *= i;
product %= n;
//System.out.println("product: " + product);
}
i++;
}
if(product > 1) {
list.remove(product);
}
System.out.println(list.size());
for (Long integer : list) System.out.print(integer + " ");
}
public static long gcd(long a, long b) {
if(b==0) return a;
return gcd(b,a%b);
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 3b59f9355f1f2b7955be9b6d57f473d8 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* multiplication of all co-prime numbers of n is also co-prime.
*/
public class Main {
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch(IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
long nextLong() {
return Long.parseLong(next());
}
}
public static void main(String[] args) {
FastReader fs = new FastReader();
long n = fs.nextLong();
//co prime numbers
//and then modulo n
//if ans 1 good, else if ans p then remove p
List<Long> list = new ArrayList<>();
long i = 1;
long product = 1;
while(i < n) {
if(gcd(n,i)==1) {
list.add(i);
product *= i;
product %= n;
//System.out.println("product: " + product);
}
i++;
}
if(product > 1) {
for(int j = 0; j < list.size(); j++) {
if(list.get(j) == product) {
list.remove(product);
break;
}
}
}
System.out.println(list.size());
for (Long integer : list) System.out.print(integer + " ");
}
public static long gcd(long a, long b) {
if(b==0) return a;
return gcd(b,a%b);
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 33975c1379ffffe3627f4b81043aeef7 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Product1_ModuloN {
public static int find_GCD(int a, int b) {
if (a == 0)
return b;
return find_GCD(b % a, a);
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
boolean[] coprimes = new boolean[n];
coprimes[1] = true;
int count = 1;
long product = 1;
for (int i = 2; i < n; i++) {
coprimes[i] = find_GCD(i, n) == 1;
if (coprimes[i]) {
product = (product * i) % n;
count++;
}
}
StringBuilder result = new StringBuilder();
if (product != 1) {
coprimes[(int) product] = false;
count--;
}
for (int i = 1; i < n; i++) {
if (coprimes[i]) {
result.append(i).append(" ");
}
}
System.out.println(count);
System.out.println(result);
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 199cff20a029538ca7cfdb683783ba71 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | /*input
8
*/
import java.io.*;
import java.util.*;
public class Main2{
static int ok[] = new int[100005];
public static void main(String[] args) throws Exception {
MyScanner scn = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
/*
int n = sc.nextInt(); // read input as integer
long k = sc.nextLong(); // read input as long
double d = sc.nextDouble(); // read input as double
String str = sc.next(); // read input as String
String s = sc.nextLine(); // read whole line as String
int result = 3*n;
out.println(result); // print via PrintWriter
*/
//The Code Starts here
StringBuilder sb = new StringBuilder();
int n = scn.nextInt();
long prod = 1;
int last = 0;
for(int i=1;i<n;i++){
if(gcd(i,n) == 1){
ok[i] = 1;
prod = (prod*i)%n;
last = i;
}
}
if(prod != 1){
ok[n-1] = 0;
}
int count = 0;
for(int i=1;i<n;i++){
if(ok[i] == 1){
count++;
}
}
sb.append(count + "\n");
for(int i=1;i<n;i++){
if(ok[i] == 1){
sb.append(i + " ");
}
}
System.out.print(sb);
//The Code Ends here
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public int[][] nextInt2DArray(int m, int n) {
int[][] arr = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
arr[i][j] = nextInt();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
public static void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
public static class Pair implements Comparable<Pair> {
long u;
long v;
public Pair(long u, long v) {
this.u = u;
this.v = v;
}
public int hashCode() {
int hu = (int) (u ^ (u >>> 32));
int hv = (int) (v ^ (v >>> 32));
return 31 * hu + hv;
}
public boolean equals(Object o) {
Pair other = (Pair) o;
return u == other.u && v == other.v;
}
public int compareTo(Pair other) {
return Long.compare(u, other.u) != 0 ? Long.compare(u, other.u) : Long.compare(v, other.v);
}
public String toString() {
return "[u=" + u + ", v=" + v + "]";
}
}
//is number prime upto N numbers
static int N = 10;
static boolean[] prime_seive = new boolean[N+1];
public static void seive(){
Arrays.fill(prime_seive, true);
prime_seive[0] = false;
prime_seive[1] = false;
for(int i=2;i*i<N;i++){
if(prime_seive[i] == true){
for(int j=i*i;j<=N;j+=i){
prime_seive[j] = false;
}
}
}
}
public static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
//
//--------------------------------------------------------
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 6605c0e5f6f3ffef84bacbbdeea86fda | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | /*input
8
*/
import java.io.*;
import java.util.*;
public class Main2{
static int ok[] = new int[100005];
public static void main(String[] args) throws Exception {
MyScanner scn = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
/*
int n = sc.nextInt(); // read input as integer
long k = sc.nextLong(); // read input as long
double d = sc.nextDouble(); // read input as double
String str = sc.next(); // read input as String
String s = sc.nextLine(); // read whole line as String
int result = 3*n;
out.println(result); // print via PrintWriter
*/
//The Code Starts here
StringBuilder sb = new StringBuilder();
int n = scn.nextInt();
long prod = 1;
int last = 0;
for(int i=1;i<n;i++){
if(gcd(i,n) == 1){
ok[i] = 1;
prod = (prod*i)%n;
last = i;
}
}
if(prod != 1){
ok[last] = 0;
}
int count = 0;
for(int i=1;i<n;i++){
if(ok[i] == 1){
count++;
}
}
sb.append(count + "\n");
for(int i=1;i<n;i++){
if(ok[i] == 1){
sb.append(i + " ");
}
}
System.out.print(sb);
//The Code Ends here
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public int[][] nextInt2DArray(int m, int n) {
int[][] arr = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
arr[i][j] = nextInt();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
public static void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
public static class Pair implements Comparable<Pair> {
long u;
long v;
public Pair(long u, long v) {
this.u = u;
this.v = v;
}
public int hashCode() {
int hu = (int) (u ^ (u >>> 32));
int hv = (int) (v ^ (v >>> 32));
return 31 * hu + hv;
}
public boolean equals(Object o) {
Pair other = (Pair) o;
return u == other.u && v == other.v;
}
public int compareTo(Pair other) {
return Long.compare(u, other.u) != 0 ? Long.compare(u, other.u) : Long.compare(v, other.v);
}
public String toString() {
return "[u=" + u + ", v=" + v + "]";
}
}
//is number prime upto N numbers
static int N = 10;
static boolean[] prime_seive = new boolean[N+1];
public static void seive(){
Arrays.fill(prime_seive, true);
prime_seive[0] = false;
prime_seive[1] = false;
for(int i=2;i*i<N;i++){
if(prime_seive[i] == true){
for(int j=i*i;j<=N;j+=i){
prime_seive[j] = false;
}
}
}
}
public static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
//
//--------------------------------------------------------
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | eb612f203cb6f2598b3cfd29a1db1ffd | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | /*input
8
*/
import java.io.*;
import java.util.*;
public class Main2{
static int ok[] = new int[100005];
public static void main(String[] args) throws Exception {
MyScanner scn = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
/*
int n = sc.nextInt(); // read input as integer
long k = sc.nextLong(); // read input as long
double d = sc.nextDouble(); // read input as double
String str = sc.next(); // read input as String
String s = sc.nextLine(); // read whole line as String
int result = 3*n;
out.println(result); // print via PrintWriter
*/
//The Code Starts here
StringBuilder sb = new StringBuilder();
int n = scn.nextInt();
long prod = 1;
for(int i=1;i<n;i++){
if(gcd(i,n) == 1){
ok[i] = 1;
prod = (prod*i)%n;
}
}
if(prod != 1){
ok[(int)prod] = 0;
}
int count = 0;
for(int i=1;i<n;i++){
if(ok[i] == 1){
count++;
}
}
sb.append(count + "\n");
for(int i=1;i<n;i++){
if(ok[i] == 1){
sb.append(i + " ");
}
}
sb.append("\n");
System.out.print(sb);
//The Code Ends here
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public int[][] nextInt2DArray(int m, int n) {
int[][] arr = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++)
arr[i][j] = nextInt();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
public static void debug(Object... o) {
System.out.println(Arrays.deepToString(o));
}
public static class Pair implements Comparable<Pair> {
long u;
long v;
public Pair(long u, long v) {
this.u = u;
this.v = v;
}
public int hashCode() {
int hu = (int) (u ^ (u >>> 32));
int hv = (int) (v ^ (v >>> 32));
return 31 * hu + hv;
}
public boolean equals(Object o) {
Pair other = (Pair) o;
return u == other.u && v == other.v;
}
public int compareTo(Pair other) {
return Long.compare(u, other.u) != 0 ? Long.compare(u, other.u) : Long.compare(v, other.v);
}
public String toString() {
return "[u=" + u + ", v=" + v + "]";
}
}
//is number prime upto N numbers
static int N = 10;
static boolean[] prime_seive = new boolean[N+1];
public static void seive(){
Arrays.fill(prime_seive, true);
prime_seive[0] = false;
prime_seive[1] = false;
for(int i=2;i*i<N;i++){
if(prime_seive[i] == true){
for(int j=i*i;j<=N;j+=i){
prime_seive[j] = false;
}
}
}
}
public static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
//
//--------------------------------------------------------
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 4e34a0ce3a1a8271d8a5623f2d7b1848 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | // package com.prituladima;
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.stream.IntStream;
public final class Template {
private void solve() {
//https://math.stackexchange.com/questions/441667/the-product-of-integers-relatively-prime-to-n-congruent-to-pm-1-pmod-n
int n = nextInt();
if (false) {
//long mul = 1;
int maxMask = -1;
for (int mask = 0; mask < (1 << n); mask++) {
long local = 1;
for (int bit = 0; bit < n; bit++) {
//[0, n) -> [1, n]
//
local *= !on(mask, bit) ? 1 : (bit + 1);
}
if (local % n == 1) {
maxMask = mask;
}
}
println(Integer.bitCount(maxMask));
for (int bit = 0; bit < n; bit++) {
//[0, n) -> [1, n]
//
if (on(maxMask, bit)) {
print(bit + 1);
print(' ');
}
}
} else {
boolean[] ans = new boolean[n + 20];
long mul = 1;
int cnt = 0 ;
for (int i = 1; i <= n; i++) {
if(gcd(i, n) == 1) {
mul *= i;
mul %= n;
ans[i] = true;
cnt++;
// println(">>> " + mul);
}
}
if (mul != 1) {
ans[(int)mul] = false;
cnt--;
}
println(cnt);
for (int i = 1; i <= n; i++) {
if (ans[i]) {
print(i);
print(' ');
}
}
}
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
private boolean on(int mask, int bit) {
return (mask & (1 << bit)) > 0;
}
public static void main(String[] args) {
new Template().run();
}
private BufferedReader reader;
private StringTokenizer tokenizer;
private PrintWriter writer;
private void run() {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out))) {
this.reader = reader;
this.writer = writer;
this.tokenizer = null;
solve();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
private int nextInt(int radix) {
return Integer.parseInt(nextToken(), radix);
}
private int nextInt() {
return Integer.parseInt(nextToken());
}
private long nextLong(int radix) {
return Long.parseLong(nextToken(), radix);
}
private long nextLong() {
return Long.parseLong(nextToken());
}
private double nextDouble() {
return Double.parseDouble(nextToken());
}
private int[] nextArr(int size) {
return Arrays.stream(new int[size]).map(c -> nextInt()).toArray();
}
private long[] nextArrL(int size) {
return Arrays.stream(new long[size]).map(c -> nextLong()).toArray();
}
private double[] nextArrD(int size) {
return Arrays.stream(new double[size]).map(c -> nextDouble()).toArray();
}
private char[][] nextCharMatrix(int n) {
return IntStream.range(0, n).mapToObj(i -> nextToken().toCharArray()).toArray(char[][]::new);
}
private int[][] nextIntMatrix(final int n, final int m) {
return IntStream.range(0, n).mapToObj(i -> nextArr(m)).toArray(int[][]::new);
}
private double[][] nextDoubleMatrix(final int n, final int m) {
return IntStream.range(0, n).mapToObj(i -> nextArrD(m)).toArray(double[][]::new);
}
private String nextToken() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
private void printf(String format, Object... args) {
writer.printf(format, args);
}
private void print(Object o) {
writer.print(o);
}
private void println() {
writer.println();
}
private void println(Object o) {
print(o);
println();
}
private void flush() {
writer.flush();
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | c7123769c1caed5fca0b0806fc668fdc | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
public class ProdMod {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Queue<Integer> fl = new LinkedList<Integer>();
fl.add(1);
String ans = "1 ";
boolean arr[] = new boolean[n];
int size = 1;
long prod = 1;
for(int i = 2; i <= n-2; i++) {
if(arr[i] == true)
continue;
else if(n % i == 0) {
for(int j = i; j <= n-2; j += i)
arr[j] = true;
}
else{
//ans = ans + i + " ";
fl.add(i);
size++;
prod = (prod*i % n);
}
}
if(prod == n-1 && n != 2) {
size++;
//ans = ans + (n-1) + " ";
fl.add(n-1);
}
System.out.println(size);
//System.out.println(ans);
for(int i = 0; i < size; i++) {
System.out.print(fl.poll()+ " ");
}
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 197b70e9ce1cf9d84c3825134f1d6e71 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
* @Author:
* @Date: 2021/4/26 22:27
*/
public class Product1ModuloN {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int n = fs.nextInt();
boolean [] used = new boolean[n];
int mul = 1;
int sz = 0;
for(int i = 1; i<=n-1; i++){
if(gcd(i, n)==1){
used[i] = true;
mul = (int)(((long)mul*(long)i)%n);
sz++;
}
}
if (mul!=1){
used[mul] = false;
sz--;
}
System.out.println(sz);
for(int i = 1; i<=n-1;i++){
if(used[i]){
System.out.print(i+" ");
}
}
}
static int gcd(int a, int b){
if(b==0){
return a;
}
return gcd(b, a%b);
}
static class FastScanner{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next(){
while (!st.hasMoreTokens()){
try {
st = new StringTokenizer(br.readLine());
}catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
long nextLong(){
return Long.parseLong(next());
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | e861bda57a8421de58c104636b85199a | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes |
import java.util.*;
public class Main {
public static int gcd(int a, int b){
if(b == 0){
return a;
}
return gcd(b,a%b);
}
public static void solve1(int n){
boolean[] arr = new boolean[n];
long prod = 1;
for(int i = 1 ; i < n ; i++){
if(gcd(i,n) == 1){
arr[i] = true;
prod = (prod * i) % n;
}
else{
arr[i] = false;
}
}
if(prod != 1){
arr[(int) prod] = false;
}
int count = 0;
for(int i = 1 ; i < n ; i++){
count += (arr[i]) ? 1 : 0;
}
System.out.println(count);
for(int i = 1 ; i < n ; i++){
if(arr[i]){
System.out.print(i + " ");
}
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = 1;
while(t-- > 0) {
int n = sc.nextInt();
solve1(n);
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 88b9ff9ce01a9d8144cc3a06d7bfc120 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class C{
public static void main(String[] args){
FastReader sc = new FastReader();
int n=sc.nextInt();
long pro=1;
ArrayList<Integer> a = new ArrayList<>();
for(int i=1;i<n;i++) {
if(gcd(i,n)==1) {
a.add(i);
pro=(pro*i)%n;
}
}
if(pro!=1) {
for(int i=0;i<a.size();i++) {
if(a.get(i)==pro) {
a.remove(i);
}
}
}
System.out.println(a.size());
for(int i:a)System.out.print(i+" ");
}
static int gcd(int a,int b) {
if(b==0) return a;
return gcd(b,a%b);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | ed0aa25a075aa308e1b0bfd10a88864f | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.Collections;
import java.util.Map.Entry;
public class div2_716_C implements Runnable
{
public void run()
{
InputReader sc = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = 1;
while(t-- > 0)
{
int n = sc.nextInt();
ArrayList<Long> al = new ArrayList<>();
long res=1;
for(int i=1;i<n;i++)
{
if(gcd(i,n)==1)
{
al.add((long)i);
res = (res*i)%n;
}
}
if(res!=1)
{
al.remove(res);
}
System.out.println(al.size());
for(int i=0;i<al.size();i++)
{
System.out.print(al.get(i)+" ");
}
}
}
boolean isPerfectSquare(int x)
{
if (x >= 0)
{
int sr = (int)Math.sqrt(x);
return ((sr * sr) == x);
}
return false;
}
boolean ispalindrome(char ch[])
{
int i=0,j=ch.length-1;
while(i<j)
{
if(ch[i]==ch[j])
{
i++;
j--;
}
else
{
return false;
}
}
return true;
}
static class sortintarray {
public static void sort(int[] arr) {
int n = arr.length, mid, h, s, l, i, j, k;
int[] res = new int[n];
n--;
for (s = 1; s <= n; s <<= 1) {
for (l = 0; l < n; l += (s << 1)) {
h = Math.min(l + (s << 1) - 1, n);
mid = Math.min(l + s - 1, n);
i = l;
j = mid + 1;
k = l;
while (i <= mid && j <= h) res[k++] = (arr[i] <= arr[j] ? arr[i++] : arr[j++]);
while (i <= mid) res[k++] = arr[i++];
while (j <= h) res[k++] = arr[j++];
for (k = l; k <= h; k++) arr[k] = res[k];
}
}
}
}
static class sortchararray {
public static void sort(char[] arr) {
int n = arr.length, mid, h, s, l, i, j, k;
char[] res = new char[n];
n--;
for (s = 1; s <= n; s <<= 1) {
for (l = 0; l < n; l += (s << 1)) {
h = Math.min(l + (s << 1) - 1, n);
mid = Math.min(l + s - 1, n);
i = l;
j = mid + 1;
k = l;
while (i <= mid && j <= h) res[k++] = (arr[i] <= arr[j] ? arr[i++] : arr[j++]);
while (i <= mid) res[k++] = arr[i++];
while (j <= h) res[k++] = arr[j++];
for (k = l; k <= h; k++) arr[k] = res[k];
}
}
}
}
long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);}
int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}
long factorial(long n){
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
boolean isPrime(int n)
{
if (n <= 1){
return false;
}
for (int i = 2; i < n; i++){
if (n % i == 0){
return false;
}
}
return true;
}
long ceil(long a,long b)
{
if(a%b==0)
{
return a/b;
}
else
{
return (a/b)+1;
}
}
int ceil(int a,int b)
{
if(a%b==0)
{
return a/b;
}
else
{
return (a/b)+1;
}
}
public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int hashCode() {
return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<U, V> p = (Pair<U, V>) o;
return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
}
public int compareTo(Pair<U, V> b) {
int cmpU = x.compareTo(b.x);
return cmpU != 0 ? cmpU : y.compareTo(b.y);
}
public String toString() {
return String.format("(%s, %s)", x.toString(), y.toString());
}
}
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new div2_716_C(),"Main",1<<27).start();
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 7676c1e28a66984e60e343d2675df109 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | /*input
4
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static PrintWriter out;
static int MOD = 1000000007;
static FastReader scan;
/*-------- I/O using short named function ---------*/
public static String ns(){return scan.next();}
public static int ni(){return scan.nextInt();}
public static long nl(){return scan.nextLong();}
public static double nd(){return scan.nextDouble();}
public static String nln(){return scan.nextLine();}
public static void p(Object o){out.print(o);}
public static void ps(Object o){out.print(o + " ");}
public static void pn(Object o){out.println(o);}
/*-------- for output of an array ---------------------*/
static void iPA(int arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
static void lPA(long arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
static void sPA(String arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
static void dPA(double arr []){
StringBuilder output = new StringBuilder();
for(int i=0; i<arr.length; i++)output.append(arr[i] + " ");out.println(output);
}
/*-------------- for input in an array ---------------------*/
static void iIA(int arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ni();
}
static void lIA(long arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nl();
}
static void sIA(String arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ns();
}
static void dIA(double arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nd();
}
/*------------ for taking input faster ----------------*/
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
// Method to check if x is power of 2
static boolean isPowerOfTwo (int x) { return x!=0 && ((x&(x-1)) == 0);}
//Method to return lcm of two numbers
static int gcd(int a, int b){return a==0?b:gcd(b % a, a); }
//Method to count digit of a number
static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);}
//Method for sorting
static void ruffle_sort(int[] a) {
//shandom_ruffle
Random r=new Random();
int n=a.length;
for (int i=0; i<n; i++) {
int oi=r.nextInt(n);
int temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//sort
Arrays.sort(a);
}
//Method for checking if a number is prime or not
static boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
OutputStream outputStream =System.out;
out =new PrintWriter(outputStream);
scan =new FastReader();
//for fast output sometimes
StringBuilder sb = new StringBuilder();
int t = 1;
while(t-->0){
int n = ni();
boolean check[] = new boolean[n];
long p = 1;
for(int i=1; i<n; i++){
if(gcd(n, i) == 1){
check[i] = true;
p*=i;
p %= n;
}
}
if(p != 1){
check[(int)p] = false;
}
int count = 0;
for(int i=1; i<n; i++)
if(check[i]){
count++;
sb.append(i + " ");
}
pn(count);
pn(sb);
}
out.flush();
out.close();
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 0872a4f257703553378ec14b386b7034 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer> ans = new ArrayList<>();
ans.add(1);
if( n!= 2)
{
long prod = 1;
for(int i=2; i<n-1; i++)
{
if( GCD(i, n) == 1 )
{
ans.add(i);
prod *= i;
prod = prod%n;
}
}
prod *= n-1;
if( prod % n == 1 )
ans.add(n-1);
}
System.out.println(ans.size());
for(Integer ele : ans )
System.out.print(ele + " ");
}
public static int GCD(int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | f05ff9e08b987a004ab37931bf6ea44e | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;import java.io.*;import java.math.*;
public class Main
{
public static void process()throws IOException
{
int n=ni();
ArrayList<Integer>l1=new ArrayList<>();
for(int i=1;i<=n-1;i++)
{
if(gcd(n,i)==1)
l1.add(i);
}
long prod=1;
int ind=0;
for(int i=0;i<l1.size();i++)
{
prod=(prod*l1.get(i))%n;
if(prod==1)
ind=i+1;
}
pn(ind);
for(int i=0;i<ind;i++)
p(l1.get(i)+" ");
pn("");
}
static long mod=(long)1e9+7l;
static AnotherReader sc;
static PrintWriter out;
public static void main(String[]args)throws IOException
{
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);}
else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");}
int t=1;
// t=ni();
while(t-->0) {process();}
out.flush();out.close();
}
static void pn(Object o){out.println(o);}
static void p(Object o){out.print(o);}
static void pni(Object o){out.println(o);out.flush();}
static int ni()throws IOException{return sc.nextInt();}
static long nl()throws IOException{return sc.nextLong();}
static double nd()throws IOException{return sc.nextDouble();}
static String nln()throws IOException{return sc.nextLine();}
static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;}
static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;}
static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static class AnotherReader{BufferedReader br; StringTokenizer st;
AnotherReader()throws FileNotFoundException{
br=new BufferedReader(new InputStreamReader(System.in));}
AnotherReader(int a)throws FileNotFoundException{
br = new BufferedReader(new FileReader("input.txt"));}
String next()throws IOException{
while (st == null || !st.hasMoreElements()) {try{
st = new StringTokenizer(br.readLine());}
catch (IOException e){ e.printStackTrace(); }}
return st.nextToken(); } int nextInt() throws IOException{
return Integer.parseInt(next());}
long nextLong() throws IOException
{return Long.parseLong(next());}
double nextDouble()throws IOException { return Double.parseDouble(next()); }
String nextLine() throws IOException{ String str = ""; try{
str = br.readLine();} catch (IOException e){
e.printStackTrace();} return str;}}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 571290bb33dfc74aa8ec64d9cffc6d5e | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | /*
5
*/
import java.util.*;
import java.io.*;
public class Main {
public static long N;
public static ArrayList<Long> relativelyPrime = new ArrayList<Long>();
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Long.parseLong(br.readLine());
long mult = 1;
for(long i = 1; i < N; i++){
if(gcd(N, i) == 1){
relativelyPrime.add(i);
mult = (mult * i) % N;
}
}
if(mult > 1) relativelyPrime.remove(mult);
System.out.println(relativelyPrime.size());
for(long item : relativelyPrime) System.out.print(item + " ");
System.out.println();
br.close();
}
public static long gcd(long x, long y){ // x > y
if(x % y == 0) return y;
return gcd(y, x % y);
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | c7e3f8e9347b82500d6ab7374aa68d92 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.TreeSet;
public class prod {
static int gcdAlg(int n1, int n2) {
if (n2 == 0) {
return n1;
}
return gcdAlg(n2, n1 % n2);
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
boolean[] ans = new boolean[n];
int count = 0;
long curp = 1;
for (int i = 1; i < n; i++) {
if(gcdAlg(i, n) == 1){
count++;
ans[i] = true;
curp = (curp * ((long)i)) % ((long)n);
}
}
if(curp != 1){
ans[(int) curp] = false;
count--;
}
System.out.println(count);
for (int i = 1; i < n; i++) {
if(ans[i]) System.out.print(i + " ");
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 406f5efc718522f1e1c2ac63cacc4487 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class cf1514c {
public static void main(String[] args) throws IOException {
int n = ri();
long cur = 1;
List<Integer> coprimes = new ArrayList<>();
for (int i = 1; i < n; ++i) {
if (gcd(i, n) == 1) {
coprimes.add(i);
cur *= i;
cur %= n;
}
}
if (cur != 1) {
coprimes.remove(Integer.valueOf((int) cur));
}
prln(coprimes.size());
prln(coprimes);
close();
}
static BufferedReader __i = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter __o = new PrintWriter(new OutputStreamWriter(System.out));
static StringTokenizer input;
static Random __r = new Random();
// references
// IBIG = 1e9 + 7
// IMAX ~= 2e9
// LMAX ~= 9e18
// constants
static final int IBIG = 1000000007;
static final int IMAX = 2147483647;
static final long LMAX = 9223372036854775807L;
// math util
static int minof(int a, int b, int c) {return min(a, min(b, c));}
static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static long minof(long a, long b, long c) {return min(a, min(b, c));}
static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
static int maxof(int a, int b, int c) {return max(a, max(b, c));}
static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static long maxof(long a, long b, long c) {return max(a, max(b, c));}
static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
static int fli(double d) {return (int) d;}
static int cei(double d) {return (int) ceil(d);}
static long fll(double d) {return (long) d;}
static long cel(double d) {return (long) ceil(d);}
static int gcd(int a, int b) {return b == 0 ? a : gcd(b, a % b);}
static long gcd(long a, long b) {return b == 0 ? a : gcd(b, a % b);}
static int[] exgcd(int a, int b) {if (b == 0) return new int[] {1, 0}; int[] y = exgcd(b, a % b); return new int[] {y[1], y[0] - y[1] * (a / b)};}
static long[] exgcd(long a, long b) {if (b == 0) return new long[] {1, 0}; long[] y = exgcd(b, a % b); return new long[] {y[1], y[0] - y[1] * (a / b)};}
static int randInt(int min, int max) {return __r.nextInt(max - min + 1) + min;}
static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
// array util
static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
static void rsort(int[] a) {shuffle(a); sort(a);}
static void rsort(long[] a) {shuffle(a); sort(a);}
static void rsort(double[] a) {shuffle(a); sort(a);}
static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
// input
static void r() throws IOException {input = new StringTokenizer(rline());}
static int ri() throws IOException {return Integer.parseInt(rline());}
static long rl() throws IOException {return Long.parseLong(rline());}
static double rd() throws IOException {return Double.parseDouble(rline());}
static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;}
static void ria(int[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = ni();}
static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}
static void riam1(int[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1;}
static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;}
static void rla(long[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = nl();}
static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;}
static void rda(double[] a) throws IOException {int n = a.length; r(); for (int i = 0; i < n; ++i) a[i] = nd();}
static char[] rcha() throws IOException {return rline().toCharArray();}
static void rcha(char[] a) throws IOException {int n = a.length, i = 0; for (char c : rline().toCharArray()) a[i++] = c;}
static String rline() throws IOException {return __i.readLine();}
static String n() {return input.nextToken();}
static int rni() throws IOException {r(); return ni();}
static int ni() {return Integer.parseInt(n());}
static long rnl() throws IOException {r(); return nl();}
static long nl() {return Long.parseLong(n());}
static double rnd() throws IOException {r(); return nd();}
static double nd() {return Double.parseDouble(n());}
// output
static void pr(int i) {__o.print(i);}
static void prln(int i) {__o.println(i);}
static void pr(long l) {__o.print(l);}
static void prln(long l) {__o.println(l);}
static void pr(double d) {__o.print(d);}
static void prln(double d) {__o.println(d);}
static void pr(char c) {__o.print(c);}
static void prln(char c) {__o.println(c);}
static void pr(char[] s) {__o.print(new String(s));}
static void prln(char[] s) {__o.println(new String(s));}
static void pr(String s) {__o.print(s);}
static void prln(String s) {__o.println(s);}
static void pr(Object o) {__o.print(o);}
static void prln(Object o) {__o.println(o);}
static void prln() {__o.println();}
static void pryes() {prln("yes");}
static void pry() {prln("Yes");}
static void prY() {prln("YES");}
static void prno() {prln("no");}
static void prn() {prln("No");}
static void prN() {prln("NO");}
static boolean pryesno(boolean b) {prln(b ? "yes" : "no"); return b;};
static boolean pryn(boolean b) {prln(b ? "Yes" : "No"); return b;}
static boolean prYN(boolean b) {prln(b ? "YES" : "NO"); return b;}
static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();}
static void h() {prln("hlfd"); flush();}
static void flush() {__o.flush();}
static void close() {__o.close();}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | fd9b6f6b2d9575277d3b79c170f36d79 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
CProduct1ModuloN solver = new CProduct1ModuloN();
solver.solve(1, in, out);
out.close();
}
static class CProduct1ModuloN {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
long prod = 1;
ArrayList<Integer> list = new ArrayList<>();
HashSet<Integer> set = new HashSet<>();
for (int i = 1; i < n; i++) {
if (gcd(i, n) == 1) {
prod = (prod * i) % n;
list.add(i);
set.add(i);
}
}
int s = list.size();
if (prod == 1) {
prod = -1;
} else {
s--;
}
out.println(s);
for (int a : list) {
if (a != prod) {
out.print(a + " ");
}
}
}
public static int gcd(int a, int b) {
return b == 0 ? (a < 0 ? -a : a) : gcd(b, a % b);
}
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void close() {
writer.close();
}
public void println(int i) {
writer.println(i);
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | c42ab8cd779c5662f50b95b7fdb1ed76 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class Contest716C
{
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
String next() { // reads in the next string
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() { // reads in the next int
return Integer.parseInt(next());
}
public long nextLong() { // reads in the next long
return Long.parseLong(next());
}
public double nextDouble() { // reads in the next double
return Double.parseDouble(next());
}
}
static InputReader r = new InputReader(System.in);
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args)
{
int N = r.nextInt();
int count = 0;
ArrayList<Integer> al = new ArrayList<Integer>();
long prod = 1;
for (int i = 1; i < Math.max(2,N - 1); i ++)
{
if (gcd(i,N) == 1)
{
al.add(i);
count ++;
prod *= i;
prod %= N;
}
}
if (prod != 1)
{
al.add(N - 1);
count ++;
}
pw.println(count);
for (int i: al)
{
pw.print(i);
pw.print(" ");
}
pw.close();
}
static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | ae1afee2ed3a998d75f1d2fb419624f4 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class hacker49 {
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
OutputStream outputStream =System.out;
PrintWriter out =new PrintWriter(outputStream);
FastReader s=new FastReader();
long n=s.nextLong();
ArrayList<Long> e=new ArrayList<>();
for(long i=1;i<=n-1;i++) {
if(GCD(i,n)==1) {
e.add(i);
}
}
long d=1;
int count=0;
int c=-1;
ArrayList<Long> r=new ArrayList<>();
out:for(int i=0;i<e.size();i++) {
d*=e.get(i);
d=(d%n);
r.add(e.get(i));
if(d==1 && i!=0) {
c=i;
count++;
// break out;
}
}
if(count==0) {
out.println(1);
out.println(1);
}else {
out.println(c+1);
for(int i=0;i<=c;i++) {
out.print(e.get(i)+" ");
}
}
out.close();
}
static ArrayList<Integer>[] f1=new ArrayList[200001];
static ArrayList<Integer>[] f2=new ArrayList[401];
static int vis1[]=new int[200001];
static int vis2[]=new int[401];
static int len1[]=new int[200001];
static int len2[]=new int[401];
static void bfs1(int node) {
vis1[node]=1;
PriorityQueue<pair> q=new PriorityQueue<>();
q.add(new pair(node,0));
int h=0;
while(!q.isEmpty()) {
pair f=q.poll();
for(int i=0;i<f1[f.a].size();i++) {
if(vis1[f1[f.a].get(i)]==0) {
q.add(new pair(f1[f.a].get(i),f.b+1));
vis1[f1[f.a].get(i)]=1;
len1[f1[f.a].get(i)]=f.b+1;
}
}
}
}
static void bfs2(int node) {
vis2[node]=1;
PriorityQueue<pair> q=new PriorityQueue<>();
q.add(new pair(node,0));
int h=0;
while(!q.isEmpty()) {
pair f=q.poll();
for(int i=0;i<f2[f.a].size();i++) {
if(vis2[f2[f.a].get(i)]==0) {
q.add(new pair(f2[f.a].get(i),f.b+1));
vis2[f2[f.a].get(i)]=1;
len2[f2[f.a].get(i)]=f.b+1;
}
}
}
}
public static int[] Create(int[] a,int n) {
int[] b=new int[n+1];
for(int i=1;i<=n;i++) {
int j=i;
int h=a[i];
while(i<=n) {
b[i]+=h;
i=get_next(i);
}
i=j;
}
return b;
}
public static int get_next(int a) {
return a+(a&-a);
}
public static int get_parent(int a) {
return a-(a&-a);
}
public static int query_1(int[] b,int index) {
int sum=0;
if(index<=0) {
return 0;
}
while(index>0) {
sum+=b[index];
index=get_parent(index);
}
return sum;
}
public static int query(int[] b,int n,int l,int r) {
int sum=0;
sum+=query_1(b,r);
sum-=query_1(b,l-1);
return sum;
}
public static void update(int[] a,int[] b,int n,int index,int val) {
int diff=val-a[index];
a[index]+=diff;
while(index<=n) {
b[index]+=diff;
index=get_next(index);
}
}
// public static void Create(int[] a,pair[] segtree,int low,int high,int pos) {
// if(low>high) {
// return;
// }
// if(low==high) {
// segtree[pos].b.add(a[low]);
// return ;
// }
// int mid=(low+high)/2;
// Create(a,segtree,low,mid,2*pos);
// Create(a,segtree,mid+1,high,2*pos+1);
// segtree[pos].b.addAll(segtree[2*pos].b);
// segtree[pos].b.addAll(segtree[2*pos+1].b);
// }
// public static void update(pair[] segtree,int low,int high,int index,int pos,int val,int prev) {
// if(index>high || index<low) {
// return ;
// }
// if(low==high && low==index) {
// segtree[pos].b.remove(prev);
// segtree[pos].b.add(val);
// return;
// }
// int mid=(high+low)/2;
// update(segtree,low,mid,index,2*pos,val,prev);
// update(segtree,mid+1,high,index,2*pos+1,val,prev);
// segtree[pos].b.clear();
// segtree[pos].b.addAll(segtree[2*pos].b);
// segtree[pos].b.addAll(segtree[2*pos+1].b);
//
// }
// public static pair query(pair[] segtree,int low,int high,int qlow,int qhigh ,int pos) {
// if(low>=qlow && high<=qhigh) {
// return segtree[pos];
// }
// if(qhigh<low || qlow>high) {
// return new pair();
// }
// int mid=(low+high)/2;
// pair a1=query(segtree,low,mid,qlow,qhigh,2*pos);
// pair a2=query(segtree,mid+1,high,qlow,qhigh,2*pos+1);
// pair a3=new pair();
// a3.b.addAll(a1.b);
// a3.b.addAll(a2.b);
// return a3;
//
// }
//
// public static int nextPowerOf2(int n)
// {
// n--;
// n |= n >> 1;
// n |= n >> 2;
// n |= n >> 4;
// n |= n >> 8;
// n |= n >> 16;
// n++;
// return n;
// }
//
//// static class pair implements Comparable<pair>{
//// private int a;
//// private long b;
////// private long c;
//// pair(int a,long b){
//// this.a=a;
//// this.b=b;
////// this.c=c;
//// }
//// public int compareTo(pair o) {
////// if(this.a!=o.a) {
////// return Long.compare(this.a, o.a);
////// }else {
//// return Long.compare(o.b,this.b);
////// }
//// }
//// }
static class pair implements Comparable<pair>{
private int a;
private int b;
pair(int a,int b){
// this.b=new HashSet<>();
this.a=a;
this.b=b;
}
public int compareTo(pair o) {
return Integer.compare(this.b, o.b);
}
}
public static int lower_bound(ArrayList<Long> a ,int n,long x) {
int l=0;
int r=n;
while(r>l+1) {
int mid=(l+r)/2;
if(a.get(mid)<=x) {
l=mid;
}else {
r=mid;
}
}
return l;
}
public static int[] is_prime=new int[1000001];
public static ArrayList<Long> primes=new ArrayList<>();
public static void sieve() {
long maxN=1000000;
for(long i=1;i<=maxN;i++) {
is_prime[(int) i]=1;
}
is_prime[0]=0;
is_prime[1]=0;
for(long i=2;i*i<=maxN;i++) {
if(is_prime[(int) i]==1) {
// primes.add((int) i);
for(long j=i*i;j<=maxN;j+=i) {
is_prime[(int) j]=0;
}
}
}
for(long i=0;i<=maxN;i++) {
if(is_prime[(int) i]==1) {
primes.add(i);
}
}
}
// public static pair[] merge_sort(pair[] A, int start, int end) {
// if (end > start) {
// int mid = (end + start) / 2;
// pair[] v = merge_sort(A, start, mid);
// pair[] o = merge_sort(A, mid + 1, end);
// return (merge(v, o));
// } else {
// pair[] y = new pair[1];
// y[0] = A[start];
// return y;
// }
// }
// public static pair[] merge(pair a[], pair b[]) {
// pair[] temp = new pair[a.length + b.length];
// int m = a.length;
// int n = b.length;
// int i = 0;
// int j = 0;
// int c = 0;
// while (i < m && j < n) {
// if (a[i].b >= b[j].b) {
// temp[c++] = a[i++];
//
// } else {
// temp[c++] = b[j++];
// }
// }
// while (i < m) {
// temp[c++] = a[i++];
// }
// while (j < n) {
// temp[c++] = b[j++];
// }
// return temp;
// }
public static long im(long a) {
return binary_exponentiation_1(a,mod-2)%mod;
}
public static long binary_exponentiation_1(long a,long n) {
long res=1;
while(n>0) {
if(n%2!=0) {
res=((res)%(1000000007) * (a)%(1000000007))%(1000000007);
n--;
}else {
a=((a)%(1000000007) *(a)%(1000000007))%(1000000007);
n/=2;
}
}
return (res)%(1000000007);
}
public static long bn(long a,long n) {
long res=1;
while(n>0) {
if(n%2!=0) {
res=res*a;
n--;
}else {
a*=a;
n/=2;
}
}
return res;
}
public static long[] fac=new long[100001];
public static void find_factorial() {
fac[0]=1;
fac[1]=1;
for(int i=2;i<=100000;i++) {
fac[i]=(fac[i-1]*i)%(mod);
}
}
static long mod=1000000007;
public static long GCD(long a,long b) {
if(b==(long)0) {
return a;
}
return GCD(b , a%b);
}
static long c=0;
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 5195dab3e2730835617cddb83fa46628 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | //package product1modulon;
import java.util.*;
import java.io.*;
public class product1modulon {
public static boolean isCoprime(int a, int b) {
for(int i = 2; i <= Math.min(a, b); i++) {
if(a % i == 0 && b % i == 0) {
return false;
}
while(a % i == 0) {
a /= i;
}
while(b % i == 0) {
b /= i;
}
}
return true;
}
public static boolean isPrime(int a) {
for(int i = 2; i < a; i++) {
if(a % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args) throws IOException {
BufferedReader fin = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(fin.readLine());
TreeSet<Integer> nums = new TreeSet<Integer>();
boolean isPrime = isPrime(n);
long prod = 1;
for(int i = 1; i < n; i++) {
//System.out.println(i);
if(isPrime || isCoprime(i, n)) {
nums.add(i);
prod *= (long) i;
prod %= n;
}
}
int noGood = (int) prod;
StringBuilder fout = new StringBuilder();
fout.append(nums.size() - (noGood != 1? 1 : 0)).append("\n");
for(int i : nums) {
if(i != noGood || noGood == 1) {
fout.append(i).append(" ");
}
}
System.out.println(fout);
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 7b9a229a0373deb713917e195011dc3a | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | //https://codeforces.com/contest/1514/problem/C
//C. Product 1 Modulo N
import java.util.*;
import java.io.*;
public class CF_1514_C{
static int gcd(int a, int b){
if(b==0)
return a;
return gcd(b, a%b);
}
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
StringTokenizer st;
int n = Integer.parseInt(br.readLine());
boolean coprime[] = new boolean[n];
long product = 1;
for(int i=1;i<n;i++){
if(gcd(n, i)==1){
coprime[i] = true;
product = product*i%n;
}
}
if(product!=1)
coprime[(int)product] = false;
int count = 0;
for(int i=1;i<n;i++)
if(coprime[i]){
count++;
sb.append(i+" ");
}
pw.print(count+"\n");
pw.print(sb);
pw.flush();
pw.close();
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | a33b08ec4ac6e5c6c82ee2e643bde1a8 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
public class Solve{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
ArrayList<Integer> al=new ArrayList<>();
long p=1;
for(int i=1;i<n;i++){
if(gcd(i,n)==1){
p=(p*(long)i)%n;
al.add(i);
}
}
if(p%n!=1){
System.out.println(al.size()-1);
for(int i=0;i<al.size()-1;i++)System.out.print(al.get(i)+" ");
}
else{
System.out.println(al.size());
for(int a:al)System.out.print(a+" ");
}
}
static int gcd(int a,int b){
if(b==0) return a;
else return gcd(b,a%b);
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | b1d7df1474170e36237a5fe5a656f9b0 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main {
public static void main (String[] args) {
// Scanner scan = new Scanner(System.in);
FastScanner scan = new FastScanner();
long n = scan.nextInt();
List<Long> ls = new ArrayList<>();
long prod = 1;
for(long i = 1; i <= n - 2; i++) {
if(gcd(n, i) == 1) {
ls.add(i);
prod = ((prod % n) * (i % n) % n);
}
}
prod = ((prod % n) * (n - 1)) % n;
if(prod % n == 1) {
ls.add(n - 1);
}
System.out.println(ls.size());
for(long i : ls) System.out.print(i + " ");
System.out.println();
}
static long gcd(long n1, long n2) {
while(n1 != n2) {
if(n1 > n2) {
n1 -= n2;
} else {
n2 -= n1;
}
}
return n1;
}
/*
int n = scan.nextInt();
int[] arr = scan.readArray(n);
int n = scan.nextInt();
int m = scan.nextInt();
int[][] arr = new int[n][m];
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
arr[i][j] = scan.nextInt();
}
}
*/
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 372082282e5dce74d7e20d4ff110d415 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Main {
public static void main(String[] args) {
new Main().solve(new InputReader(System.in), new PrintWriter(System.out));
}
private void solve(InputReader in, PrintWriter pw) {
int tt = 1;
// int tt = in.nextInt();
while (tt-- > 0) {
int n = in.nextInt();
// 1 modulo n => target%n==1 or target=a*n+1
long sz = 0, j = 1;
boolean[] st = new boolean[n + 1];
for (int i = 1; i < n; i++) {
if (gcd(i, n) == 1) {
j = (j * i) % n;
st[i] = true;
sz++;
}
}
if (j != 1) {
st[(int) j] = false;
sz--;
}
pw.println(sz);
for (int i = 1; i < n; i++) {
if (st[i]) {
pw.print(i + " ");
}
}
pw.println();
}
pw.close();
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
class InputReader {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
String str;
try {
str = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return str;
}
public boolean hasNext() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
String nextLine = null;
try {
nextLine = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (nextLine == null)
return false;
tokenizer = new StringTokenizer(nextLine);
}
return true;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public int[] nextArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; ++i) {
a[i] = nextInt();
}
return a;
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | a345d8b6744b612e25fbdb07a17dbc24 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
public static int mod = (int)1e9 + 7;
// **** -----> Disjoint Set Union(DSU) Start **********
public static int findPar(int node, int[] parent) {
if (parent[node] == node)
return node;
return parent[node] = findPar(parent[node], parent);
}
public static boolean union(int u, int v, int[] rank, int[] parent) {
u = findPar(u, parent);
v = findPar(v, parent);
if(u == v) return false;
if (rank[u] < rank[v])
parent[u] = v;
else if (rank[u] > rank[v])
parent[v] = u;
else {
parent[u] = v;
rank[v]++;
}
return true;
}
// **** DSU Ends ***********
//Pair with int int
public static class Pair{
public int a;
public int b;
Pair(int a , int b){
this.a = a;
this.b = b;
}
@Override
public String toString(){
return a + " -> " + b;
}
}
public static String toBinary(int n) {return Integer.toBinaryString(n);}
public static boolean isPalindrome(String s) {int i = 0, j = s.length() - 1;while (i < j) {if (s.charAt(i) != s.charAt(j))return false;i++;j--;}return true;}
public static boolean isPalindrome(int[] arr) {int i = 0, j = arr.length - 1;while (i < j) {if (arr[i] != arr[j])return false;}return true;}
public static int pow(int x, int y) {int res = 1;x = x % mod;if (x == 0)return 0;while (y > 0) {if ((y & 1) != 0)res = (res * x) % mod;y = y >> 1;x = (x * x) % mod;}return res;}
public static int gcd(int a, int b) {if (b == 0)return a;return gcd(b, a % b);}
public static long gcd(long a, long b) {if (b == 0)return a;return gcd(b, a % b);}
public static void sort(long[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);long temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);}
public static void reverse(int a[]) {int i, k, t, n = a.length;for (i = 0; i < n / 2; i++) {t = a[i];a[i] = a[n - i - 1];a[n - i - 1] = t;}}
public static void sort(int[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);int temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);}
public static void revSort(int[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);int temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);reverse(a);}
public static long LCM(long a, long b) {if (a > b) {long t = a;a = b;b = t;}a /= gcd(a, b);return (a * b);}
public static int findMax(int[] a, int left, int right) {int res = left;int max = a[left];for (int i = left + 1; i <= right; i++) {if (a[i] > max) {max = a[i];res = i;}}return res;}
public static long findClosest(long arr[], long target) {int n = arr.length;if (target <= arr[0])return arr[0];if (target >= arr[n - 1])return arr[n - 1];int i = 0, j = n, mid = 0;while (i < j) {mid = (i + j) / 2;if (arr[mid] == target)return arr[mid];if (target < arr[mid]) {if (mid > 0 && target > arr[mid - 1])return getClosest(arr[mid - 1], arr[mid], target);j = mid;} else {if (mid < n - 1 && target < arr[mid + 1])return getClosest(arr[mid], arr[mid + 1], target);i = mid + 1;}}return arr[mid];}
public static long getClosest(long val1, long val2, long target) {if (target - val1 >= val2 - target)return val2;else return val1;}
public static int findClosest(int arr[], int target) { int n = arr.length; if (target <= arr[0]) return arr[0]; if (target >= arr[n - 1]) return arr[n - 1]; int i = 0, j = n, mid = 0; while (i < j) { mid = (i + j) / 2; if (arr[mid] == target) return arr[mid];if (target < arr[mid]) {if (mid > 0 && target > arr[mid - 1])return getClosest(arr[mid - 1], arr[mid], target);j = mid;} else {if (mid < n - 1 && target < arr[mid + 1])return getClosest(arr[mid], arr[mid + 1], target);i = mid + 1;}}return arr[mid];}
public static int getClosest(int val1, int val2, int target) {if (target - val1 >= val2 - target)return val2;else return val1;}
public static String reverse(String str) {StringBuilder sb = new StringBuilder(str);sb.reverse();return sb.toString();}
public static boolean isPrime(int n){if (n <= 1)return false;if (n <= 3)return true;if (n % 2 == 0 || n % 3 == 0)return false;for (int i = 5; i * i <= n; i = i + 6)if (n % i == 0 || n % (i + 2) == 0)return false;return true;}
public static int xorSum(int arr[], int n){int bits = 0;for (int i = 0; i < n; ++i)bits |= arr[i];int ans = bits * (int)Math.pow(2, n-1);return ans;}
public static ArrayList<Integer> primeFactors(int n) { ArrayList<Integer> res = new ArrayList<>();while (n%2 == 0) { res.add(2); n = n/2; } for (int i = 3; i <= Math.sqrt(n); i = i+2) { while (n%i == 0) { res.add(i); n = n/i; } } if (n > 2) res.add(n);return res;}
public static ArrayList<Long> primeFactors(long n) { ArrayList<Long> res = new ArrayList<>();while (n%2 == 0) { res.add(2L); n = n/2; } for (long i = 3; i <= Math.sqrt(n); i = i+2) { while (n%i == 0) { res.add(i); n = n/i; } } if (n > 2) res.add(n);return res;}
static int lower_bound(int array[], int low, int high, int key){
int mid;
while (low < high) {
mid = low + (high - low) / 2;
if (key <= array[mid]) high = mid;
else low = mid + 1;
}
if (low < array.length && array[low] < key) low++;
return low;
}
static long fastPower(long a, long b, long mod){
long ans = 1 ;
while (b > 0)
{
if ((b&1) != 0) ans = (ans%mod * a%mod) %mod;
b >>= 1 ;
a = (a%mod * a%mod)%mod ;
}
return ans ;
}
/********************************* Start Here ***********************************/
// int mod = 1000000007;
static HashMap<String, Integer> map;
static Set<Integer> set1, set2;
static boolean[] vis;
public static void main(String[] args) throws java.lang.Exception {
if (System.getProperty("ONLINE_JUDGE") == null) {
PrintStream ps = new PrintStream(new File("output.txt"));
System.setOut(ps);
}
FastScanner sc = new FastScanner("input.txt");
StringBuilder result = new StringBuilder();
// sieveOfEratosthenes(1000000);
// int T = sc.nextInt();
int T = 1;
for(int test = 1; test <= T; test++){
int n = sc.nextInt();
List<Long> list = new ArrayList<>();
long prod = 1;
for(long i=1;i<=n;i++) {
if(gcd(n, i) == 1) {
list.add(i);
prod = (prod*i)%n;
}
}
StringBuilder ans = new StringBuilder();
if(prod != 1) {
List<Long> temp = new ArrayList<>();
for(long x:list) {
if(x == prod)
continue;
temp.add(x);
}
list = temp;
}
for(int i=0;i<list.size();i++) {
ans.append(list.get(i) + " ");
}
result.append(list.size()+"\n"+ans+"\n");
}
System.out.println(result);
System.out.close();
}
public static long solve(long[] a, long[] b, long[] pre, int i, int j){
long dif = 0;
long sum = 0;
while(i >= 0 && j < a.length){
sum += a[i]*b[j];
if(i != j) sum += a[j]*b[i];
dif = Math.max(dif, sum-(pre[j]-pre[i]+a[i]*b[i]));
i--; j++;
}
return pre[a.length-1]+dif;
}
// static void sieveOfEratosthenes(int n){
// boolean prime[] = new boolean[n + 1];
// for (int i = 0; i <= n; i++)
// prime[i] = true;
// for (int p = 2; p * p <= n; p++){
// if (prime[p] == true){
// for (int i = p * p; i <= n; i += p)
// prime[i] = false;
// }
// }
// for (int i = 2; i <= n; i++){
// if (prime[i] == true)
// set.add(i);
// }
// }
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
if (st == null || !st.hasMoreTokens()) {
try {
return br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken("\n");
}
public String[] readStringArray(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++) {
a[i] = next();
}
return a;
}
int nextInt() {
return Integer.parseInt(nextToken());
}
String next() {
return nextToken();
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
int[][] read2dArray(int n, int m) {
int[][] a = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = nextInt();
}
}
return a;
}
long[][] read2dlongArray(int n, int m) {
long[][] a = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = nextLong();
}
}
return a;
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 6b8cb719fa88c1ab7981d2956420c3e5 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.*;
import java.lang.Math;
import java.util.*;
public final class Codechef {
static BufferedReader br = new BufferedReader(
new InputStreamReader(System.in)
);
static BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(System.out)
);
static StringTokenizer st;
static int mod = 1000000000;
/*write your constructor and global variables here*/
static class sortCond implements Comparator<Pair<Integer, Integer>> {
@Override
public int compare(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) {
if (p1.a <= p2.a) {
return -1;
} else {
return 1;
}
}
}
static class sortCond1 implements Comparator<Integer> {
@Override
public int compare(Integer p1, Integer p2) {
if (p1 <= p2) {
return 1;
} else {
return -1;
}
}
}
static class Rec {
int a;
int b;
long c;
Rec(int a, int b, long c) {
this.a = a;
this.b = b;
this.c = c;
}
}
static class Pair<f, s> {
f a;
s b;
Pair(f a, s b) {
this.a = a;
this.b = b;
}
}
interface modOperations {
int mod(int a, int b, int mod);
}
static int findBinaryExponentian(int a, int pow, int mod) {
if (pow == 1) {
return a;
} else if (pow == 0) {
return 1;
} else {
int retVal = findBinaryExponentian(a, (int) pow / 2, mod);
int val = (pow % 2 == 0) ? 1 : a;
return modMul.mod(modMul.mod(retVal, retVal, mod), val, mod);
}
}
static int findPow(int a, int b, int mod) {
if (b == 1) {
return a % mod;
} else if (b == 0) {
return 1;
} else {
int res = findPow(a, (int) b / 2, mod);
return modMul.mod(res, modMul.mod(res, (b % 2 == 1 ? a : 1), mod), mod);
}
}
static int bleft(int ele, int[] arr) {
int l = 0;
int h = arr.length - 1;
int ans = -1;
while (l <= h) {
int mid = l + (int) (h - l) / 2;
int val = arr[mid];
if (ele > val) {
l = mid + 1;
} else if (ele < val) {
h = mid - 1;
} else {
ans = mid;
h = mid - 1;
}
}
return ans;
}
static int gcd(int a, int b) {
int div = b;
int rem = a % b;
while (rem != 0) {
int temp = rem;
rem = div % rem;
div = temp;
}
return div;
}
static long[] log(long no, long n) {
long i = 1;
int cnt = 0;
long sum = 0l;
long arr[] = new long[2];
while (i < no) {
sum += i;
cnt++;
if (sum == n) {
arr[0] = 1l * cnt;
arr[1] = sum;
break;
}
i *= 2l;
}
if (arr[0] == 0) {
arr[0] = cnt;
arr[1] = sum;
}
return arr;
}
static modOperations modAdd = (int a, int b, int mod) -> {
return (a % mod + b % mod) % mod;
};
static modOperations modSub = (int a, int b, int mod) -> {
return (int) ((1l * a % mod - 1l * b % mod + 1l * mod) % mod);
};
static modOperations modMul = (int a, int b, int mod) -> {
return (int) ((1l * (a % mod) * 1l * (b % mod)) % (1l * mod));
};
static modOperations modDiv = (int a, int b, int mod) -> {
return modMul.mod(a, findBinaryExponentian(b, mod - 1, mod), mod);
};
static HashSet<Integer> primeList(int MAXI) {
int[] prime = new int[MAXI + 1];
HashSet<Integer> obj = new HashSet<>();
for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) {
if (prime[i] == 0) {
obj.add(i);
for (int j = i * i; j <= MAXI; j += i) {
prime[j] = 1;
}
}
}
for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) {
if (prime[i] == 0) {
obj.add(i);
}
}
return obj;
}
static int[] factorialList(int MAXI, int mod) {
int[] factorial = new int[MAXI + 1];
factorial[0] = factorial[1] = 1;
factorial[2] = 2;
for (int i = 3; i < MAXI + 1; i++) {
factorial[i] = modMul.mod(factorial[i - 1], i, mod);
}
return factorial;
}
static void put(HashMap<Integer, Integer> cnt, int key) {
if (cnt.containsKey(key)) {
cnt.replace(key, cnt.get(key) + 1);
} else {
cnt.put(key, 1);
}
}
static long arrSum(ArrayList<Long> arr) {
long tot = 0;
for (int i = 0; i < arr.size(); i++) {
tot += arr.get(i);
}
return tot;
}
static int ord(char b) {
return (int) b - (int) 'a';
}
static int optimSearch(int[] cnt, int lower_bound, int pow, int n) {
int l = lower_bound + 1;
int h = n;
int ans = 0;
while (l <= h) {
int mid = l + (h - l) / 2;
if (cnt[mid] - cnt[lower_bound] == pow) {
return mid;
} else if (cnt[mid] - cnt[lower_bound] < pow) {
ans = mid;
l = mid + 1;
} else {
h = mid - 1;
}
}
return ans;
}
static Pair<Long, Integer> ret_ans(ArrayList<Integer> ans) {
int size = ans.size();
int mini = 1000000000 + 1;
long tit = 0l;
for (int i = 0; i < size; i++) {
tit += 1l * ans.get(i);
mini = Math.min(mini, ans.get(i));
}
return new Pair<>(tit - mini, mini);
}
static int factorList(
HashMap<Integer, Integer> maps,
int no,
int maxK,
int req
) {
int i = 1;
while (i * i <= no) {
if (no % i == 0) {
if (i != no / i) {
put(maps, no / i);
}
put(maps, i);
if (maps.get(i) == req) {
maxK = Math.max(maxK, i);
}
if (maps.get(no / i) == req) {
maxK = Math.max(maxK, no / i);
}
}
i++;
}
return maxK;
}
static ArrayList<Integer> getKeys(HashMap<Integer, Integer> maps) {
ArrayList<Integer> vals = new ArrayList<>();
for (Map.Entry<Integer, Integer> map : maps.entrySet()) {
vals.add(map.getKey());
}
return vals;
}
static ArrayList<Integer> getValues(HashMap<Integer, Integer> maps) {
ArrayList<Integer> vals = new ArrayList<>();
for (Map.Entry<Integer, Integer> map : maps.entrySet()) {
vals.add(map.getValue());
}
return vals;
}
static int getMax(ArrayList<Integer> arr) {
int max = arr.get(0);
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i) > max) {
max = arr.get(i);
}
}
return max;
}
static int getMin(ArrayList<Integer> arr) {
int max = arr.get(0);
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i) < max) {
max = arr.get(i);
}
}
return max;
}
static void bitRep(int n, int no) throws IOException {
int curr = (int) Math.pow(2, n - 1);
for (int i = n - 1; i >= 0; i--) {
if ((curr & no) != 0) {
bw.write("1");
} else {
bw.write("0");
}
curr /= 2;
}
bw.write("\n");
}
static ArrayList<Integer> retPow(int MAXI) {
long curr = 1l;
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 1; i <= MAXI; i++) {
curr *= 2l;
curr = curr % mod;
arr.add((int) curr);
}
return arr;
}
static String is(int no) {
return Integer.toString(no);
}
static String ls(long no) {
return Long.toString(no);
}
static int pi(String s) {
return Integer.parseInt(s);
}
static long pl(String s) {
return Long.parseLong(s);
}
/*write your methods and classes here*/
public static void main(String[] args) throws IOException {
int cases = 1, n, i;
while (cases-- != 0) {
n = pi(br.readLine());
if (n == 2 || n == 3) {
bw.write("1\n1\n");
} else {
ArrayList<Integer> arr = new ArrayList<>();
long pro = 1l;
for (i = 1; i < n; i++) {
if (gcd(n, i) == 1) {
pro = (pro * 1l * i) % n;
arr.add(i);
}
}
//bw.write(is(arr.size()) + "\n");
ArrayList<Integer> res = new ArrayList<>();
for (i = 0; i < arr.size(); i++) {
if (pro == 1 || pro != arr.get(i)) {
res.add(arr.get(i));
}
}
bw.write(is(res.size()) + "\n");
for (i = 0; i < res.size(); i++) {
bw.write(is(res.get(i)) + " ");
}
bw.write("\n");
}
}
bw.flush();
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 2135a65e4e9dde1040b0ac59c3e6455f | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class practise {
static boolean multipleTC = false;
final static int Mod = 1000000007;
final static int Mod2 = 998244353;
final double PI = 3.14159265358979323846;
int MAX = 1000000007;
void pre() throws Exception {
}
void DFSUtil(int v, boolean[] visited, ArrayList<Integer> al, List<Integer> adj[]) {
visited[v] = true;
al.add(v);
// System.out.print(v + " ");
Iterator<Integer> it = adj[v].iterator();
while (it.hasNext()) {
int n = it.next();
if (!visited[n])
DFSUtil(n, visited, al, adj);
}
}
void DFS(List<Integer> adj[], ArrayList<ArrayList<Integer>> components, int V) {
boolean[] visited = new boolean[V];
for (int i = 0; i < V; i++) {
ArrayList<Integer> al = new ArrayList<>();
if (!visited[i]) {
DFSUtil(i, visited, al, adj);
components.add(al);
}
}
}
long gcd(long a, long b) {
if(b == 0)
return a;
return gcd(b, a%b);
}
void solve(int t) throws Exception {
long n = nl();
List<Long> list = new ArrayList<>();
long prod = 1;
for(long i=1;i<=n;i++) {
if(gcd(n, i) == 1) {
list.add(i);
prod = (prod*i)%n;
}
}
StringBuilder ans = new StringBuilder();
if(prod != 1) {
List<Long> temp = new ArrayList<>();
for(long x:list) {
if(x == prod)
continue;
temp.add(x);
}
list = temp;
}
for(int i=0;i<list.size();i++) {
ans.append(list.get(i) + " ");
}
pn(list.size());
pn(ans);
}
double dist(int x1, int y1, int x2, int y2) {
double a = x1 - x2, b = y1 - y2;
return Math.sqrt((a * a) + (b * b));
}
long xor_sum_upton(long n) {
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
return 0;
}
int[] readArr(int n) throws Exception {
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
void sort(int arr[], int left, int right) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = left; i <= right; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = left; i <= right; i++)
arr[i] = list.get(i - left);
}
void sort(int arr[]) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < arr.length; i++)
arr[i] = list.get(i);
}
public long max(long... arr) {
long max = arr[0];
for (long itr : arr)
max = Math.max(max, itr);
return max;
}
public int max(int... arr) {
int max = arr[0];
for (int itr : arr)
max = Math.max(max, itr);
return max;
}
public long min(long... arr) {
long min = arr[0];
for (long itr : arr)
min = Math.min(min, itr);
return min;
}
public int min(int... arr) {
int min = arr[0];
for (int itr : arr)
min = Math.min(min, itr);
return min;
}
public long sum(long... arr) {
long sum = 0;
for (long itr : arr)
sum += itr;
return sum;
}
public long sum(int... arr) {
long sum = 0;
for (int itr : arr)
sum += itr;
return sum;
}
String bin(long n) {
return Long.toBinaryString(n);
}
String bin(int n) {
return Integer.toBinaryString(n);
}
static int bitCount(int x) {
return x == 0 ? 0 : (1 + bitCount(x & (x - 1)));
}
static void dbg(Object... o) {
System.err.println(Arrays.deepToString(o));
}
int bit(long n) {
return (n == 0) ? 0 : (1 + bit(n & (n - 1)));
}
int abs(int a) {
return (a < 0) ? -a : a;
}
long abs(long a) {
return (a < 0) ? -a : a;
}
void p(Object o) {
out.print(o);
}
void pn(Object o) {
out.println(o);
}
void pni(Object o) {
out.println(o);
out.flush();
}
void pn(int[] arr) {
int n = arr.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(arr[i] + " ");
}
pn(sb);
}
void pn(long[] arr) {
int n = arr.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(arr[i] + " ");
}
pn(sb);
}
String n() throws Exception {
return in.next();
}
String nln() throws Exception {
return in.nextLine();
}
char c() throws Exception {
return in.next().charAt(0);
}
int ni() throws Exception {
return Integer.parseInt(in.next());
}
long nl() throws Exception {
return Long.parseLong(in.next());
}
double nd() throws Exception {
return Double.parseDouble(in.next());
}
public static void main(String[] args) throws Exception {
new practise().run();
}
FastReader in;
PrintWriter out;
void run() throws Exception {
in = new FastReader();
out = new PrintWriter(System.out);
int T = (multipleTC) ? ni() : 1;
pre();
for (int t = 1; t <= T; t++)
solve(t);
out.flush();
out.close();
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws Exception {
br = new BufferedReader(new FileReader(s));
}
String next() throws Exception {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new Exception(e.toString());
}
}
return st.nextToken();
}
String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
throw new Exception(e.toString());
}
return str;
}
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 067c4eb5064228f4767d6895da0818b9 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
public class Main {
static BiFunction<Integer, Integer, Integer> ADD = (x, y) -> (x + y);
static BiFunction<ArrayList<Integer>, ArrayList<Integer>, ArrayList<Integer>> ADD_ARRAY_LIST = (x, y) -> {
x.addAll(y);
return x;
};
static Function<Pair<Integer, Integer>, Integer> GET_FIRST = (x) -> (x.first);
static Function<Pair<Integer, Integer>, Integer> GET_SECOND = (x) -> (x.second);
static Comparator<Pair<Integer, Integer>> C = Comparator.comparing(x -> x.first + x.second);
static long MOD = 1_000_000_000 + 7;
public static void main(String[] args) throws Exception {
long startTime = System.nanoTime();
int t = 1;
while (t-- > 0) {
solve();
}
long endTime = System.nanoTime();
err.println("Execution Time : +" + (endTime - startTime) / 1000000 + " ms");
exit(0);
}
static void solve() {
int n = in.nextInt();
long prod = 1;
Set<Long> res = new TreeSet<>();
for (long i = 1; i < n; i++) {
if (gcd(i, n) == 1) {
res.add(i);
prod *= i;
prod %= n;
}
}
if (prod > 1) {
res.remove(prod);
}
out.println(res.size());
for (long a : res) {
out.print(a + " ");
}
out.println("");
}
static ArrayList<Integer> divisors(int a) {
ArrayList<Integer> div = new ArrayList<Integer>();
for (int i = 2; i * i <= a; i++) {
if (a % i == 0) {
div.add(i);
if (i * i != a) {
div.add(a / i);
}
}
}
return div;
}
static void debug(Object... args) {
for (Object a : args) {
out.println(a);
}
}
static int dist(Pair<Integer, Integer> a, Pair<Integer, Integer> b) {
return Math.abs(a.first - b.first) + Math.abs(a.second - b.second);
}
static void y() {
out.println("YES");
}
static void n() {
out.println("NO");
}
static int[] stringToArray(String s) {
return s.chars().map(x -> Character.getNumericValue(x)).toArray();
}
static <T> T min(T a, T b, Comparator<T> C) {
if (C.compare(a, b) <= 0) {
return a;
}
return b;
}
static <T> T max(T a, T b, Comparator<T> C) {
if (C.compare(a, b) >= 0) {
return a;
}
return b;
}
static void fail() {
out.println("-1");
}
static class Pair<T, R> {
public T first;
public R second;
public Pair(T first, R second) {
this.first = first;
this.second = second;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final Pair<?, ?> pair = (Pair<?, ?>) o;
return Objects.equals(first, pair.first) && Objects.equals(second, pair.second);
}
@Override
public int hashCode() {
return Objects.hash(first, second);
}
@Override
public String toString() {
return "Pair{" + "a=" + first + ", b=" + second + '}';
}
public T getFirst() {
return first;
}
public R getSecond() {
return second;
}
}
static <T, R> Pair<T, R> make_pair(T a, R b) {
return new Pair<>(a, b);
}
static long mod_inverse(long a, long m) {
Number x = new Number(0);
Number y = new Number(0);
extended_gcd(a, m, x, y);
return (m + x.v % m) % m;
}
static long extended_gcd(long a, long b, Number x, Number y) {
long d = a;
if (b != 0) {
d = extended_gcd(b, a % b, y, x);
y.v -= (a / b) * x.v;
} else {
x.v = 1;
y.v = 0;
}
return d;
}
static class Number {
long v = 0;
public Number(long v) {
this.v = v;
}
}
static long lcm(long a, long b, long c) {
return lcm(a, lcm(b, c));
}
static long lcm(long a, long b) {
long p = 1L * a * b;
return p / gcd(a, b);
}
static long gcd(long a, long b) {
while (b != 0) {
long t = b;
b = a % b;
a = t;
}
return a;
}
static long gcd(long a, long b, long c) {
return gcd(a, gcd(b, c));
}
static class ArrayUtils {
static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void swap(char[] a, int i, int j) {
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void print(char[] a) {
for (char c : a) {
out.print(c);
}
out.println("");
}
static int[] reverse(int[] data) {
int[] p = new int[data.length];
for (int i = 0, j = data.length - 1; i < data.length; i++, j--) {
p[i] = data[j];
}
return p;
}
static void prefixSum(long[] data) {
for (int i = 1; i < data.length; i++) {
data[i] += data[i - 1];
}
}
static void prefixSum(int[] data) {
for (int i = 1; i < data.length; i++) {
data[i] += data[i - 1];
}
}
static long[] reverse(long[] data) {
long[] p = new long[data.length];
for (int i = 0, j = data.length - 1; i < data.length; i++, j--) {
p[i] = data[j];
}
return p;
}
static char[] reverse(char[] data) {
char[] p = new char[data.length];
for (int i = 0, j = data.length - 1; i < data.length; i++, j--) {
p[i] = data[j];
}
return p;
}
static int[] MergeSort(int[] A) {
if (A.length > 1) {
int q = A.length / 2;
int[] left = new int[q];
int[] right = new int[A.length - q];
System.arraycopy(A, 0, left, 0, q);
System.arraycopy(A, q, right, 0, A.length - q);
int[] left_sorted = MergeSort(left);
int[] right_sorted = MergeSort(right);
return Merge(left_sorted, right_sorted);
} else {
return A;
}
}
static int[] Merge(int[] left, int[] right) {
int[] A = new int[left.length + right.length];
int i = 0;
int j = 0;
for (int k = 0; k < A.length; k++) {
// To handle left becoming empty
if (i == left.length && j < right.length) {
A[k] = right[j];
j++;
continue;
}
// To handle right becoming empty
if (j == right.length && i < left.length) {
A[k] = left[i];
i++;
continue;
}
if (left[i] <= right[j]) {
A[k] = left[i];
i++;
} else {
A[k] = right[j];
j++;
}
}
return A;
}
static long[] MergeSort(long[] A) {
if (A.length > 1) {
int q = A.length / 2;
long[] left = new long[q];
long[] right = new long[A.length - q];
System.arraycopy(A, 0, left, 0, q);
System.arraycopy(A, q, right, 0, A.length - q);
long[] left_sorted = MergeSort(left);
long[] right_sorted = MergeSort(right);
return Merge(left_sorted, right_sorted);
} else {
return A;
}
}
static long[] Merge(long[] left, long[] right) {
long[] A = new long[left.length + right.length];
int i = 0;
int j = 0;
for (int k = 0; k < A.length; k++) {
// To handle left becoming empty
if (i == left.length && j < right.length) {
A[k] = right[j];
j++;
continue;
}
// To handle right becoming empty
if (j == right.length && i < left.length) {
A[k] = left[i];
i++;
continue;
}
if (left[i] <= right[j]) {
A[k] = left[i];
i++;
} else {
A[k] = right[j];
j++;
}
}
return A;
}
static int upper_bound(int[] data, int num, int start) {
int low = start;
int high = data.length - 1;
int mid = 0;
int ans = -1;
while (low <= high) {
mid = (low + high) / 2;
if (data[mid] < num) {
low = mid + 1;
} else if (data[mid] >= num) {
high = mid - 1;
ans = mid;
}
}
if (ans == -1) {
return 100000000;
}
return data[ans];
}
static int lower_bound(int[] data, int num, int start) {
int low = start;
int high = data.length - 1;
int mid = 0;
int ans = -1;
while (low <= high) {
mid = (low + high) / 2;
if (data[mid] <= num) {
low = mid + 1;
ans = mid;
} else if (data[mid] > num) {
high = mid - 1;
}
}
if (ans == -1) {
return 100000000;
}
return data[ans];
}
}
static boolean[] primeSieve(int n) {
boolean[] primes = new boolean[n + 1];
Arrays.fill(primes, true);
primes[0] = false;
primes[1] = false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (primes[i]) {
for (int j = i * i; j <= n; j += i) {
primes[j] = false;
}
}
}
return primes;
}
// Iterative Version
static HashMap<Integer, Boolean> subsets_sum_iter(int[] data) {
HashMap<Integer, Boolean> temp = new HashMap<Integer, Boolean>();
temp.put(data[0], true);
for (int i = 1; i < data.length; i++) {
HashMap<Integer, Boolean> t1 = new HashMap<Integer, Boolean>(temp);
t1.put(data[i], true);
for (int j : temp.keySet()) {
t1.put(j + data[i], true);
}
temp = t1;
}
return temp;
}
static HashMap<Integer, Integer> subsets_sum_count(int[] data) {
HashMap<Integer, Integer> temp = new HashMap<>();
temp.put(data[0], 1);
for (int i = 1; i < data.length; i++) {
HashMap<Integer, Integer> t1 = new HashMap<>(temp);
t1.merge(data[i], 1, ADD);
for (int j : temp.keySet()) {
t1.merge(j + data[i], temp.get(j) + 1, ADD);
}
temp = t1;
}
return temp;
}
static class Graph {
ArrayList<Integer>[] g;
boolean[] visited;
ArrayList<Integer>[] graph(int n) {
g = new ArrayList[n];
visited = new boolean[n];
for (int i = 0; i < n; i++) {
g[i] = new ArrayList<>();
}
return g;
}
void BFS(int s) {
Queue<Integer> Q = new ArrayDeque<>();
visited[s] = true;
Q.add(s);
while (!Q.isEmpty()) {
int v = Q.poll();
for (int a : g[v]) {
if (!visited[a]) {
visited[a] = true;
Q.add(a);
}
}
}
}
}
static class SparseTable {
int[] log;
int[][] st;
public SparseTable(int n, int k, int[] data, BiFunction<Integer, Integer, Integer> f) {
log = new int[n + 1];
st = new int[n][k + 1];
log[1] = 0;
for (int i = 2; i <= n; i++) {
log[i] = log[i / 2] + 1;
}
for (int i = 0; i < data.length; i++) {
st[i][0] = data[i];
}
for (int j = 1; j <= k; j++)
for (int i = 0; i + (1 << j) <= data.length; i++)
st[i][j] = f.apply(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
public int query(int L, int R, BiFunction<Integer, Integer, Integer> f) {
int j = log[R - L + 1];
return f.apply(st[L][j], st[R - (1 << j) + 1][j]);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 2048);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] readAllInts(int n) {
int[] p = new int[n];
for (int i = 0; i < n; i++) {
p[i] = in.nextInt();
}
return p;
}
public int[] readAllInts(int n, int s) {
int[] p = new int[n + s];
for (int i = s; i < n + s; i++) {
p[i] = in.nextInt();
}
return p;
}
public long[] readAllLongs(int n) {
long[] p = new long[n];
for (int i = 0; i < n; i++) {
p[i] = in.nextLong();
}
return p;
}
public long[] readAllLongs(int n, int s) {
long[] p = new long[n + s];
for (int i = s; i < n + s; i++) {
p[i] = in.nextLong();
}
return p;
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static void exit(int a) {
out.close();
err.close();
System.exit(a);
}
static InputStream inputStream = System.in;
static OutputStream outputStream = System.out;
static OutputStream errStream = System.err;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(outputStream);
static PrintWriter err = new PrintWriter(errStream);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 9e4e3186b4a4cf090d678639f0efe7db | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Main{
public static void main(String[] args){
Solve Flamboyance=new Solve();
}
}
class Solve{
int gcd(int a, int b){
if(b==0)
return a;
return gcd(b,a%b);
}
Solve(){
FastReader in=new FastReader();
int n=in.nextInt();
long pr=1;
Vector<Integer> v=new Vector<Integer>();
for(int i=1; i<n; i++){
if(gcd(n,i)==1){
pr=(pr*(long)i)%n;
v.add(i);
}
}
pr=pr%n;
if(pr!=1){
for(int i=0; i<v.size(); i++)
if(pr==v.get(i))
{v.remove(i); break;}
}
System.out.println(v.size());
for(int i=0; i<v.size(); i++){
System.out.print(v.get(i)+" ");
}
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){ return Integer.parseInt(next()); }
long nextLong(){ return Long.parseLong(next()); }
double nextDouble(){ return Double.parseDouble(next()); }
String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 459cfd149509b0f14c229d8d67eb9e0a | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import javax.swing.event.TreeSelectionEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.math.BigInteger;
public class Main {
static long mod = 1000000007;
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int n = fs.nextInt();
ArrayList <Integer> ar = new ArrayList <>();
long pro = 1;
for(int i = 1; i < n; i++) {
if(gcd(n,i) == 1) {
pro = ((pro % n)*(i % n)) % n;
ar.add(i);
}
}
//System.out.println(pro);
if(pro == 1) {
System.out.println(ar.size());
for(int i = 0; i < ar.size(); i++) {
System.out.print(ar.get(i) + " ");
}
}
else {
System.out.println(ar.size()-1);
for(int i = 0; i < ar.size()-1; i++) {
System.out.print(ar.get(i) + " ");
}
}
}
static int gcd(int a, int b) {
if(b == 0) return a;
return gcd(b, a%b);
}
static class pair <Key, Value> {
private Key key;
private Value value;
public pair() {
}
public pair(Key key,Value value) {
this.key = key;
this.value = value;
}
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 16891ba8081056ce4487550a0c6374d3 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class Div21514C {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
long n = Integer.parseInt(br.readLine());
ArrayList<Long> vals = new ArrayList<Long>();
vals.add(1L);
long test = 1;
for(long i = 2; i < n; i++) {
if(gcd(n, i) == 1) {
vals.add(i);
test *= i;
test %= n;
}
}
if(test != 1)
vals.remove(new Long(test));
System.out.println(vals.size());
vals.forEach(val -> System.out.print(val + " "));
}
static long gcd (long a, long b) {
if (b == 0)
return a;
else
return gcd (b, a % b);
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | ec9a0e1978b68c37eac82ab9730f4975 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes |
/*
TASK: template
LANG: JAVA
*/
import java.io.*;
import java.lang.*;
import java.util.*;
public class C1514 {
public static void main(String[] args) throws IOException{
StringBuffer ans = new StringBuffer();
StringTokenizer st;
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
long prod = 1;
ArrayList<Long> arrl = new ArrayList<>();
for(long i = 1; i <= n; i++){
if(gcdByEuclidsAlgorithm(i, n) == 1){
prod*=i;
prod%=n;
arrl.add(i);
}
}
f.close();
int index = 0;
for(int i = 0; i < arrl.size(); i++)
if(arrl.get(i) == prod)
index = i;
if(prod != 1){
arrl.remove(index);
}
ans.append(arrl.size()).append("\n");
for(Long i : arrl)
ans.append(i).append(" ");
System.out.println(ans);
}
public static long gcdByEuclidsAlgorithm(long n1, long n2) {
if (n2 == 0) {
return n1;
}
return gcdByEuclidsAlgorithm(n2, n1 % n2);
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | e3f2fd3db0210240455d8e204b9a33dd | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | // The product of all the numbers co-prime with n is also co-prime with n.
// If (product%n)!=1 , then we remove (product%n) from our list or the last element of the list if the list is sorted in asc order.
import java.io.*;
import java.util.*;
public class ProductOneModuloN {
static long gcd(long a, long b)
{
if(a==0)
return b;
return gcd(b%a,a);
}
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
long n=sc.nextLong();
List<Long> list=new ArrayList<>();
long mul=1;
for(long i=1;i<n;i++)
{
if(gcd(i,n)==1)
{
list.add(i);
mul*=i;
mul%=n;
}
}
if(mul%n==1)
{
System.out.println(list.size());
for(int i=0;i<list.size();i++)
System.out.print(list.get(i)+" ");
System.out.println();
}
else
{
System.out.println((list.size()-1));
for(int i=0;i<list.size()-1;i++)
System.out.print(list.get(i)+" ");
System.out.println();
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | ee60b0d27f0680745786b22ebe9e43cf | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class cf1519_Div2_C {
public static void main(String args[]) throws IOException {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
long p = 1;
boolean[] valid = new boolean[n];
int c = 0;
for (int i = 1; i < n; i++) {
if (gcd(i, n) == 1) {
p = (p * i) % n;
valid[i] = true;
c++;
}
}
if (p != 1) {
valid[(int)p] = false;
c--;
}
out.println(c);
for (int i = 0; i < n; i++)
if (valid[i])
out.print(i + " ");
out.println();
out.close();
}
public static int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream i) {
br = new BufferedReader(new InputStreamReader(i));
st = new StringTokenizer("");
}
public String next() throws IOException {
if(st.hasMoreTokens())
return st.nextToken();
else
st = new StringTokenizer(br.readLine());
return next();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
//#
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
//$
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 656caf71e0d297bd271d115bdecdee7d | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | //import kotlin.reflect.jvm.internal.impl.load.kotlin.JvmType;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/*
@author kalZor
*/
public class TaskC {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
int testCount = 1;
// testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class Solver {
public int gcd(int a,int b){
if(a==0) return b;
else return gcd(b%a,a);
}
public void solve(int testNumber, FastReader in, PrintWriter out) {
int n = in.nextInt();
ArrayList<Integer> a = new ArrayList<>();
a.add(1);
long prd = 1;
for(int i=2;i<n;i++){
if(gcd(i,n)==1){
prd = (prd*i)%n;
a.add(i);
}
}
if(prd!=1){
out.println(a.size()-1);
for(int x:a){
if(x!=prd) out.print(x+" ");
}
out.println();
return;
}
out.println(a.size());
for(int x:a) out.print(x+" ");
out.println();
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader(InputStream stream){
br = new BufferedReader(new
InputStreamReader(stream));
}
public String next(){
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}
catch (IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt(){
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
public String nextLine(){
String str = "";
try{
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
public int[] nextArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | c0ca2a8c0ff70e14169dd0ec13a21a3b | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Problem_3 {
public static void main(String args[]) {
// System.out.println(getGcd(98765, 92077));
FastScanner sc = new FastScanner();
int n = sc.nextInt();
List<Integer> num = new ArrayList<>();
long p = 1;
boolean finding = true;
for (int i = 1; i < n; i++) {
if (getGcd(n, i) == 1) {
num.add(i);
if (p != 0 && (p * i) % n == 0) {
System.out.println("i : " + i + ", p : " + p);
}
p = (p * i) % n;
}
}
// System.out.println("p : " + p);
int remove = -1;
if (p != 1) {
for (int i = 0; i < num.size(); i++) {
if (num.get(i) == p) {
p = 1;
remove = i;
}
}
num.remove(remove);
}
System.out.println(num.size());
for (int i = 0; i < num.size(); i++) {
System.out.print(num.get(i) + " ");
}
}
public static int getGcd(int a, int b) {
if (a == b) {
return a;
}
if (a < b) {
int temp = a;
a = b;
b = temp;
}
if (b == 0) {
return a;
}
return getGcd(a % b, b);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | e071598e9ff746d79671ef94cd6dc5d6 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class First {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
//int a = 1;
int t;
//t = in.nextInt();
t = 1;
while (t > 0) {
//out.print("Case #"+(a++)+": ");
solver.call(in,out);
t--;
}
out.close();
}
static class TaskA {
public void call(InputReader in, PrintWriter out) {
int n , b = 1;
long a = 1;
n = in.nextInt();
for (int i = 1; i < n; i++) {
if (gcd(i, n) != 1) {
continue;
}
a = (a % n * (long) i % n) % n;
if (a == 1) {
b = i;
}
}
ArrayList<Integer> arrayList = new ArrayList<>();
for (int i = 1; i <= b; i++) {
if(gcd(i,n)==1){
arrayList.add(i);
}
}
out.println(arrayList.size());
for (Integer i:arrayList) {
out.print(i+" ");
}
}
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static class answer implements Comparable<answer>{
int a;
int b;
public answer(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(answer o) {
return o.a - this.a;
}
@Override
public boolean equals(Object o){
if(o instanceof answer){
answer c = (answer)o;
return a == c.a && b == c.b;
}
return false;
}
}
static class answer1 implements Comparable<answer1>{
int a, b, c;
public answer1(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public int compareTo(answer1 o) {
if(o.c==this.c){
return this.a - o.a;
}
return o.c - this.c;
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static final Random random=new Random();
static void shuffleSort(int[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 4b0c2947c3f67bd8bcf7e4c2f2b35c6d | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solve {
static final int MOD = 1000000007;
public static void main(String[] args) {
Reader in = new Reader();
PrintWriter out = new PrintWriter(System.out);
int t = 1;
while (t-- > 0)
solve(in, out);
out.close();
}
private static void solve(Reader in, PrintWriter out) {
int n=in.readInt();
ArrayList<Integer> set=new ArrayList<>();
long product=1;
for(int i=1;i<n;i++){
if(gcd(i, n)==1){
set.add(i);
product = (product*i)%n;
}
}
if(product!=1) {
for(int i=0;i<set.size();i++) {
if(set.get(i)==product) {
set.remove(i);
}
}
}
System.out.println(set.size());
for(int i:set)System.out.print(i+" ");
}
private static int upperBound(int[] arr, int key) {
int low = 0;
int high = arr.length - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (arr[mid] <= key) {
low = mid + 1;
} else
high = mid;
}
return low;
}
private static long power(long n, long p, long m) {
long result = 1;
while (p > 0) {
if (p % 2 == 1)
result = (result * n) % m;
n = (n * n) % m;
p >>= 1;
}
return result;
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
private static void shuffleSort(int a[]) {
Random random = new Random();
int n = a.length;
for (int i = 0; i < n; i++) {
int t = random.nextInt(n);
int x = a[t];
a[t] = a[i];
a[i] = x;
}
Arrays.sort(a);
}
private static void sieve(int n) {
boolean[] prime = new boolean[n];
for (int i = 0; i < n; i++)
prime[i] = true;
for (int i = 2; i * i < n; i++) {
if (prime[i]) {
for (int j = i * i; j < n; j += i)
prime[j] = false;
}
}
for (int i = 2; i < prime.length; i++) {
if (prime[i])
System.out.print(i + " ");
}
}
private static void segmentedSieve(int low, int high) {
int sqrt = (int) Math.sqrt(high);
int[] prime = new int[sqrt + 1];
boolean[] arr = new boolean[sqrt + 1];
for (int i = 0; i <= sqrt; i++)
arr[i] = true;
int k = 0;
for (int i = 2; i <= sqrt; i++) { // generate all prime numbers till square root of high
if (arr[i]) {
prime[k] = i;
k++;
for (int j = i * i; j <= sqrt; j += i)
arr[j] = false;
}
}
// System.out.println(Arrays.toString(prime));
int diff = high - low + 1; // arr size of required length
arr = new boolean[diff];
for (int i = 0; i < diff; i++)
arr[i] = true;
for (int i = 0; i < k; i++) { // mark false to multiple of prime numbers in the range of low to high
int div = (low / prime[i]) * prime[i]; // It gives multiple of prime[i] nearest to low
if (div < low || div == prime[i])
div += prime[i];
for (int j = div; j <= high; j += prime[i]) {
if (j != prime[i])
arr[j - low] = false;
}
}
for (int i = 0; i < diff; i++) { // print prime numbers in the given segment
if (arr[i] && (i + low) != 1) {
System.out.print(i + low + " ");
}
}
System.out.println();
}
static class Pair implements Comparable<Pair> {
int a;
int b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Pair o) {
if (this.a > o.a)
return this.a - o.a;
else if (this.a < o.a)
return this.b - o.b;
else
return 0;
}
}
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String read() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int readInt() {
return Integer.parseInt(read());
}
String readLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
long readLong() {
return Long.parseLong(read());
}
float readFloat(){
return Float.parseFloat(read());
}
double readDouble() {
return Double.parseDouble(read());
}
int[] readIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = readInt();
return arr;
}
long[] readLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = readLong();
return arr;
}
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 39caadbb94f58d41b877a4c0aaa0a9ce | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class C {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
long product = 1;
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (gcd(i, n) == 1) {
ans.add(i);
product *= i;
product %= n;
}
}
if (product == 1)
System.out.println(ans.size());
else
System.out.println(ans.size() - 1);
for (int i : ans) {
if (i != product || i == 1)
System.out.print(i + " ");
}
}
static int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 36b8321ad734b59ca67fcb9fda0acdc5 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Sol{
public static void main(String []args){
FastReader sc=new FastReader(System.in);
int t=1;
while(t-->0){
int n=sc.nextInt();
StringBuilder s1=new StringBuilder();int j=0;
for(int i=1;i<=n-1;i++){if(gcd(n,i)==1){j++;}}
int res[]=new int[j];j=0;
for(int i=1;i<=n-1;i++){if(gcd(n,i)==1){res[j]=i;j++;}}
long ans=1;for(int i=0;i<j;i++){ans*=res[i];ans%=n;}
if(ans==1){System.out.println(j);for(int i=0;i<j;i++){System.out.print(res[i]+" ");}}
else{System.out.println(j-1);for(int i=0;i<j;i++){if(res[i]!=ans)System.out.print(res[i]+" ");}}
System.out.println();
}
}
static int gcd(int x,int y){if(y==0)return x;else return gcd(y,x%y);}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader(InputStream in)
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
public String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
public double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | c52a9eed69e58589d855ab77073b607e | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | //package contest19april;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class C {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static long gcd(long a,long b) {
if(b==0) {
return a;
}
return gcd(b,a%b);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FastReader sc=new FastReader();
int n=sc.nextInt();
ArrayList<Long> a=new ArrayList<>();
long prod=1;
for(long i=1;i<n;i++) {
if(gcd(i,n)==1) {
a.add(i);
prod=(prod*i)%n;
}
}
if(prod!=1) {
a.remove(Long.valueOf(prod));
}
System.out.println(a.size());
for(long x:a) {
System.out.print(x+" ");
}
System.out.println();
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 79456e87b1f68354698869dd54bf3cb3 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
public class mod
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int n;
n=sc.nextInt();
ArrayList<Integer> list = new ArrayList<>();
long pro=1;
for(int i=1;i<=n;i++)
{
if(gcd(n,i)==1)
{
list.add(i);
pro=(pro*i)%n;
}
}
if(pro==1)
{
System.out.println(list.size());
int[] arr = list.stream().mapToInt(i->i).toArray();
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
}
else
{
System.out.println(list.size()-1);
int[] arr = list.stream().mapToInt(i->i).toArray();
for(int i=0;i<arr.length;i++)
{
if(arr[i]==pro)
{
continue;
}
System.out.print(arr[i]+" ");
}
}
}
static int gcd(int a, int b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 061d92e4e8c9c026fd74791311188112 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
long n = in.nextLong();
LinkedList<BigInteger> bigIntegers = new LinkedList<>();
BigInteger bigInteger = BigInteger.valueOf(1);
for (long i = 1; i < n; i++) {
if (BigInteger.valueOf(n).gcd(BigInteger.valueOf(i)).equals(BigInteger.ONE)) {
bigInteger = bigInteger.multiply(BigInteger.valueOf(i));
bigInteger = bigInteger.mod(BigInteger.valueOf(n));
bigIntegers.add(BigInteger.valueOf(i));
}
}
if (BigInteger.valueOf(bigInteger.longValue()).equals(BigInteger.ONE)) {
int bs = bigIntegers.size();
out.println(bs);
for (BigInteger integer : bigIntegers) {
out.print(integer + " ");
}
} else {
bigIntegers.remove(bigInteger);
int bs = bigIntegers.size();
out.println(bs);
for (BigInteger integer : bigIntegers) {
out.print(integer + " ");
}
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 5c23d053e8554bd90678179829d52afe | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.math.*;
import java.util.*;
import java.io.*;
public class main
{
static Map<String,Integer> map=new HashMap<>();
public static void main(String[] args)
{
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
final int mod=1000000007;
int test_case =1;
StringBuilder sb=new StringBuilder();
for(int t1=0;t1<test_case;t1++)
{
int n=in.nextInt();
List<Integer> ls=new ArrayList();
ls.add(1);
long mul=1;
for(int i=2;i<n-1;i++)
{
if(GCD(i,n)==1)
{
ls.add(i);
mul*=i;
mul%=n;
// System.out.println(i+" "+mul+" "+mul%n);
}
}
// System.out.println(mul+" "+mul%n);
if(n>2){
// System.out.println((mul*(n-1))%n);
if((mul*(n-1))%n==1)
{
ls.add(n-1);
// System.out.println(n-1);
}
}
System.out.println(ls.size());
for(int i:ls)
{
System.out.print(i+" ");
}
}
pw.flush();
pw.close();
}
static int factorial(int n,int mod)
{
long res = 1;
for (int i=2; i<=n; i++)
res=((res%mod)*(i%mod))%mod;
return (int)res;
}
static int length(long l)
{
int c=0;
while(l>0)
{
c++;
l/=2;
}
return c;
}
static int CeilIndex(int A[], int l, int r, int key)
{
while (r - l > 1) {
int m = l + (r - l) / 2;
if (A[m] <= key)
r = m;
else
l = m;
}
return r;
}
static int LongestIncreasingSubsequenceLength(int A[], int size)
{
// Add boundary case, when array size is one
int[] tailTable = new int[size];
int len; // always points empty slot
tailTable[0] = A[0];
len = 1;
for (int i = 1; i < size; i++) {
if (A[i] > tailTable[0])
tailTable[0] = A[i];
else if (A[i] <= tailTable[len - 1])
tailTable[len++] = A[i];
else
tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i];
}
return len;
}
static int[] next(int arr[], int n) {
Stack<Integer> S = new Stack<>();
int[] next=new int[n];
for (int i = n-1; i >= 0; i--) {
while (!S.empty() && arr[S.peek()] <= arr[i]) {
S.pop();
}
if (S.empty()) {
next[i]=-1;
}
else
{
next[i]=S.peek();
}
S.push(i);
}
return next;
}
static int[] back(int arr[], int n) {
Stack<Integer> S = new Stack<>();
int[] back=new int[n];
for (int i = 0; i < n; i++) {
while (!S.empty() && arr[S.peek()] < arr[i]) {
S.pop();
}
if (S.empty()) {
back[i]=-1;
}
else
{
back[i]=S.peek();
}
S.push(i);
}
return back;
}
static class InputReader
{
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int snext()
{
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars)
{
curChar = 0;
try
{
snumChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n)
{
int a[] = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt();
}
return a;
}
public long[] nextlongArray(int n)
{
long a[] = new long[n];
for (int i = 0; i < n; i++)
{
a[i] = nextLong();
}
return a;
}
public String readString()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine()
{
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c)
{
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static long mod = 1000000007;
public static int d;
public static int p;
public static int q;
public static int[] suffle(int[] a,Random gen)
{
int n = a.length;
for(int i=0;i<n;i++)
{
int ind = gen.nextInt(n-i)+i;
int temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
return a;
}
public static void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
public static HashSet<Integer> primeFactorization(int n)
{
HashSet<Integer> a =new HashSet<Integer>();
for(int i=2;i*i<=n;i++)
{
while(n%i==0)
{
a.add(i);
n/=i;
}
}
if(n!=1)
a.add(n);
return a;
}
public static void sieve(boolean[] isPrime,int n)
{
for(int i=1;i<n;i++)
isPrime[i] = true;
isPrime[0] = false;
isPrime[1] = false;
for(int i=2;i*i<n;i++)
{
if(isPrime[i] == true)
{
for(int j=(2*i);j<n;j+=i)
isPrime[j] = false;
}
}
}
public static int GCD(int a,int b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static long GCD(long a,long b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static void extendedEuclid(int A,int B)
{
if(B==0)
{
d = A;
p = 1 ;
q = 0;
}
else
{
extendedEuclid(B, A%B);
int temp = p;
p = q;
q = temp - (A/B)*q;
}
}
public static long LCM(long a,long b)
{
return (a*b)/GCD(a,b);
}
public static int LCM(int a,int b)
{
return (a*b)/GCD(a,b);
}
public static int binaryExponentiation(int x,int n)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
public static long binaryExponentiation(long x,long n)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static int modularExponentiation(int x,int n,int M)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static long modularExponentiation(long x,long n,long M)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static int modInverse(int A,int M)
{
return modularExponentiation(A,M-2,M);
}
public static long modInverse(long A,long M)
{
return modularExponentiation(A,M-2,M);
}
public static boolean isPrime(int n)
{
if (n <= 1) return false;
if (n <= 3) return true;
if (n%2 == 0 || n%3 == 0)
return false;
for (int i=5; i*i<=n; i=i+6)
{
if (n%i == 0 || n%(i+2) == 0)
return false;
}
return true;
}
static class Pair
{
long x;
int y;
}
static class pair implements Comparable<pair>
{
Integer x, y;
pair(int x,int y)
{
this.x=x;
this.y=y;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if(result==0)
result = y.compareTo(o.y);
return result;
}
public String toString()
{
return x+" "+y;
}
public boolean equals(Object o)
{
if (o instanceof pair)
{
pair p = (pair)o;
return (Math.abs(p.x-x)==0 && Math.abs(p.y-y)==0);
}
return false;
}
public int hashCode()
{
return new Long(x).hashCode()*31 + new Long(y).hashCode();
}
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 2fe88b04886749aa2d19257ed47793a0 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.*;
public class C {
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastScanner f = new FastScanner();
PrintWriter p = new PrintWriter(System.out);
int n = f.nextInt();
boolean ans[] = new boolean[n + 1];
long pr = 1;
int count = 0;
for (int i = 1; i <= n; i++) {
if (gcd(i, n) == 1) {
ans[i] = true;
pr = (pr * i) % n;
count++;
}
}
if (pr == 1) {
p.println(count);
for (int i = 1; i < n; i++) {
if (ans[i] == true) {
p.print(i + " ");
}
}
} else {
ans[(int) pr] = false;
p.println(count - 1);
for (int i = 1; i < n; i++) {
if (ans[i] == true) {
p.print(i + " ");
}
}
}
p.close();
}
// ------------------------------------------------------------------------------------------------
// makes the prefix sum array
static long[] prefixSum(long arr[], int n) {
long psum[] = new long[n];
psum[0] = arr[0];
for (int i = 1; i < n; i++) {
psum[i] = psum[i - 1] + arr[i];
}
return psum;
}
// ------------------------------------------------------------------------------------------------
// makes the suffix sum array
static long[] suffixSum(long arr[], int n) {
long ssum[] = new long[n];
ssum[n - 1] = arr[n - 1];
for (int i = n - 2; i >= 0; i--) {
ssum[i] = ssum[i + 1] + arr[i];
}
return ssum;
}
//------------------------------------------------------------------------------------------
// BINARY EXPONENTIATION OF A NUMBER MODULO M FASTER METHOD WITHOUT RECURSIVE
// OVERHEADS
static long m = (long) (1e9 + 7);
static long binPower(long a, long n, long m) {
if (n == 0)
return 1;
long res = 1;
while (n > 0) {
if ((n & 1) != 0) {
res *= a;
}
a *= a;
n >>= 1;
}
return res;
}
//-------------------------------------------------------------------------------------------
// gcd
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
//------------------------------------------------------------------------------------------
// lcm
static long lcm(long a, long b) {
return a / gcd(a, b) * b;
}
//------------------------------------------------------------------------------------------
// BRIAN KERNINGHAM TO CHECK NUMBER OF SET BITS
// O(LOGn)
static int setBits(int n) {
int count = 0;
while (n > 0) {
n = n & (n - 1);
count++;
}
return count;
}
//------------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------------
// 0 based indexing
static boolean KthBitSet(int n, int k) {
int mask = 1;
mask = mask <<= k;
if ((mask & n) != 0)
return true;
else
return false;
}
//------------------------------------------------------------------------------------------
// EXTENDED EUCLIDEAN THEOREM
// TO REPRESENT GCD IN TERMS OF A AND B
// gcd(a,b) = a.x + b.y where x and y are integers
static long x = -1;
static long y = -1;
static long gcdxy(long a, long b) {
if (b == 0) {
x = 1;
y = 0;
return a;
} else {
long d = gcdxy(b, a % b);
long x1 = y;
long y1 = x - (a / b) * y;
x = x1;
y = y1;
return d;
}
}
//-------------------------------------------------------------------------------------------------F
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 6e8c697b12ab2837fd04444d9ca03459 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | /**
* Created by Himanshu
**/
import java.util.*;
import java.io.*;
import java.math.*;
public class C1514 {
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
Reader s = new Reader();
int n = s.i();
ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
list1.add(1);
list2.add(1);
for (int i = 2; i < n ; i++) {
int x = gcd(n, i);
if (x == 1) {
long y = ((long) (i)) * i;
if (y % n != 1) list1.add(i);
else list2.add(i);
}
}
boolean val1 = fun(list1,n);
boolean val2 = fun(list2,n);
if (val1 && val2) {
int i=1 , j = 0;
out.println(list1.size()+list2.size()-1);
while (i < list1.size() && j < list2.size()) {
int x = list1.get(i);
int y = list2.get(j);
if (x < y) {
out.print(x + " ");
i++;
} else {
out.print(y + " ");
j++;
}
}
while (i < list1.size()) {
int x = list1.get(i);
out.print(x + " ");
i++;
}
while (j < list2.size()) {
int y = list2.get(j);
out.print(y + " ");
j++;
}
} else if (val1) {
int i=0;
out.println(list1.size());
while (i < list1.size()) {
int x = list1.get(i);
out.print(x + " ");
i++;
}
} else if (val2) {
int j=0;
out.println(list2.size());
while (j < list2.size()) {
int y = list2.get(j);
out.print(y + " ");
j++;
}
} else {
out.println(1);
out.println(1);
}
// if (list1.size() > list2.size()) {
//
// if (val == 1) {
// out.println(list1.size());
// for (int x : list1) out.print(x + " ");
// } else {
// val = 1;
// for (int x : list2) val = (val*x)%n;
// if (val == 1) {
// out.println(list2.size());
// for (int x : list2) out.print(x + " ");
// } else {
// out.println(1);
// out.println(1);
// }
// }
// } else {
// long val = 1;
// for (int x : list2) val = (val*x)%n;
// if (val == 1) {
// out.println(list2.size());
// for (int x : list2) out.print(x + " ");
// } else {
// val = 1;
// for (int x : list1) val = (val*x)%n;
// if (val == 1) {
// out.println(list1.size());
// for (int x : list1) out.print(x + " ");
// } else {
// out.println(1);
// out.println(1);
// }
// }
// }
out.flush();
}
private static boolean fun(ArrayList<Integer> list , int n) {
long val = 1;
for (int x : list) val = (val*x)%n;
return val == 1;
}
public static void shuffle(long[] arr) {
int n = arr.length;
Random rand = new Random();
for (int i = 0; i < n; i++) {
long temp = arr[i];
int randomPos = i + rand.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = temp;
}
}
private static int gcd(int a, int b) {
if(b == 0) return a;
return gcd(b,a%b);
}
public static long nCr(long[] fact, long[] inv, int n, int r, long mod) {
if (n < r)
return 0;
return ((fact[n] * inv[n - r]) % mod * inv[r]) % mod;
}
private static void factorials(long[] fact, long[] inv, long mod, int n) {
fact[0] = 1;
inv[0] = 1;
for (int i = 1; i <= n; ++i) {
fact[i] = (fact[i - 1] * i) % mod;
inv[i] = power(fact[i], mod - 2, mod);
}
}
private static long power(long a, long n, long p) {
long result = 1;
while (n > 0) {
if (n % 2 == 0) {
a = (a * a) % p;
n /= 2;
} else {
result = (result * a) % p;
n--;
}
}
return result;
}
private static long power(long a, long n) {
long result = 1;
while (n > 0) {
if (n % 2 == 0) {
a = (a * a);
n /= 2;
} else {
result = (result * a);
n--;
}
}
return result;
}
private static long query(long[] tree, int in, int start, int end, int l, int r) {
if (start >= l && r >= end) return tree[in];
if (end < l || start > r) return 0;
int mid = (start + end) / 2;
long x = query(tree, 2 * in, start, mid, l, r);
long y = query(tree, 2 * in + 1, mid + 1, end, l, r);
return x + y;
}
private static void update(int[] arr, long[] tree, int in, int start, int end, int idx, int val) {
if (start == end) {
tree[in] = val;
arr[idx] = val;
return;
}
int mid = (start + end) / 2;
if (idx > mid) update(arr, tree, 2 * in + 1, mid + 1, end, idx, val);
else update(arr, tree, 2 * in, start, mid, idx, val);
tree[in] = tree[2 * in] + tree[2 * in + 1];
}
private static void build(int[] arr, long[] tree, int in, int start, int end) {
if (start == end) {
tree[in] = arr[start];
return;
}
int mid = (start + end) / 2;
build(arr, tree, 2 * in, start, mid);
build(arr, tree, 2 * in + 1, mid + 1, end);
tree[in] = (tree[2 * in + 1] + tree[2 * in]);
}
static class Reader {
private InputStream mIs;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public Reader() {
this(System.in);
}
public Reader(InputStream is) {
mIs = is;
}
public int read() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = mIs.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String s() {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long l() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int i() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public double d() throws IOException {
return Double.parseDouble(s());
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public int[] arr(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++) {
ret[i] = i();
}
return ret;
}
public long[] arrLong(int n) {
long[] ret = new long[n];
for (int i = 0; i < n; i++) {
ret[i] = l();
}
return ret;
}
}
// static class pairLong implements Comparator<pairLong> {
// long first, second;
//
// pairLong() {
// }
//
// pairLong(long first, long second) {
// this.first = first;
// this.second = second;
// }
//
// @Override
// public int compare(pairLong p1, pairLong p2) {
// if (p1.first == p2.first) {
// if(p1.second > p2.second) return 1;
// else return -1;
// }
// if(p1.first > p2.first) return 1;
// else return -1;
// }
// }
// static class pair implements Comparator<pair> {
// int first, second;
//
// pair() {
// }
//
// pair(int first, int second) {
// this.first = first;
// this.second = second;
// }
//
// @Override
// public int compare(pair p1, pair p2) {
// if (p1.first == p2.first) return p1.second - p2.second;
// return p1.first - p2.first;
// }
// }
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | aa8ac074ca68ace7a606d3b277ef66c3 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
public class C_Rnd716_Product_faster
{
static int n;
static boolean debug = false;
public static void main(String[] args) throws NumberFormatException, IOException
{
BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
if(debug) runtest();
n = Integer.parseInt(scan.readLine());
ArrayList<Integer> factors = run(n);
out.println(factors.size());
out.print(factors.get(0));
for(int i = 1; i < factors.size(); i++)
out.print(" " + factors.get(i));
out.println();
out.flush();
}
public static ArrayList<Integer> run(int n)
{
ArrayList<Integer> factors = new ArrayList<Integer>();
for(int i = 1; i < n; i++)
{
factors.add(i);
}
solve(factors);
return factors;
}
private static void solve(ArrayList<Integer> list)
{
long remainder = 1;
for(int i = list.size() - 1; i >= 0; i--)
if(gcd(n, list.get(i)) != 1)
list.remove(i);
for(int i = 0; i < list.size(); i++)
remainder = (remainder * list.get(i)) % n;
if(remainder != 1)
{
if(debug)
System.out.println("\tHad to update " + n);
list.remove(list.size() - 1);
}
}
private static int gcd(int a, int b)
{
if(a == b || b == 0) return a;
return gcd(b, a%b);
}
public static void runtest()
{
for(int i = 2; i <= 100000; i++)
{
System.out.println("Running test #" + i);
run((n = i));
}
System.out.println("Got through the test without exiting");
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 715f1a8fa73553a5efab12f12bf5dab1 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public final class C {
public static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
private static long[] bezout(long a, long b) {
if (b == 0) {
return new long[] { 1, 0 };
}
final long[] res = bezout(b, a % b);
return new long[] { res[1], res[0] - a / b * res[1] };
}
private static long modInverse(long a, long mod) {
return (a + mod) % mod;
}
public static void main(String[] args) {
final FastScanner fs = new FastScanner();
final int n = fs.nextInt();
long total = 1;
final boolean[] added = new boolean[n];
int count = 0;
for (int i = 1; i < n; i++) {
if (gcd(i, n) == 1) {
count++;
added[i] = true;
total = (total * i) % n;
}
}
if (total != 1) {
for (int i = 0; i < n; i++) {
if (added[i]) {
final long modInverse = modInverse(bezout(i, n)[0], n);
if ((modInverse * total) % n == 1) {
count--;
added[i] = false;
break;
}
}
}
}
final StringBuilder sb = new StringBuilder();
sb.append(count);
sb.append('\n');
for (int i = 0; i < n; i++) {
if (added[i]) {
sb.append(i);
sb.append(' ');
}
}
System.out.println(sb);
// for (int mask = 0; mask < (1 << 6); mask ++) {
// int curr = 1;
// for (int i = 0; i < 6; i++) {
// if((mask & (1 << i)) != 0) {
// curr *= (i + 1);
// }
// }
// System.out.println(curr + " " + (curr % 7) + " " + Integer.bitCount(mask) + " " + Integer.toBinaryString(mask));
// }
// for (int test = 0; test < t; test++) {
// final int n = fs.nextInt();
// System.out.println(n);
// }
}
static final class Utils {
private static class Shuffler {
private static void shuffle(int[] x) {
final Random r = new Random();
for (int i = 0; i <= x.length - 2; i++) {
final int j = i + r.nextInt(x.length - i);
swap(x, i, j);
}
}
private static void shuffle(long[] x) {
final Random r = new Random();
for (int i = 0; i <= x.length - 2; i++) {
final int j = i + r.nextInt(x.length - i);
swap(x, i, j);
}
}
private static void swap(int[] x, int i, int j) {
final int t = x[i];
x[i] = x[j];
x[j] = t;
}
private static void swap(long[] x, int i, int j) {
final long t = x[i];
x[i] = x[j];
x[j] = t;
}
}
public static void shuffleSort(int[] arr) {
Shuffler.shuffle(arr);
Arrays.sort(arr);
}
public static void shuffleSort(long[] arr) {
Shuffler.shuffle(arr);
Arrays.sort(arr);
}
private Utils() {}
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
private String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
//noinspection CallToPrintStackTrace
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] nextIntArray(int n) {
final int[] a = new int[n];
for (int i = 0; i < n; i++) { a[i] = nextInt(); }
return a;
}
long[] nextLongArray(int n) {
final long[] a = new long[n];
for (int i = 0; i < n; i++) { a[i] = nextLong(); }
return a;
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 77de5d60ff0ec99d75f7d3c838a4e3ed | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class product1ModuloN {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
List<Long> al = new ArrayList<>();
long p = (long)1;
for(int i =1; i<n; i++) {
if(GCD(i, n)==1) {
al.add((long)i);
p = (((long)p%(long)n)*((long)i%(long)n))%(long)n;
}
}
if(p!=(long)1) {
int ans = al.size()-1;
System.out.println(ans);
for(int i =0; i<al.size(); i++) {
if(al.get(i)==p) {
}
else{
System.out.print(al.get(i)+" ");
}
}
System.out.println();
}
else {
System.out.println(al.size());
for(int i =0; i<al.size(); i++) {
System.out.print(al.get(i)+" ");
}
System.out.println();
}
}
static int GCD(int a, int b) {
if (b == 0) {
return a;
}
return GCD(b, a % b);
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | d857b0d4891ca8b1dffd9365757b2a46 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF1514C
{
public static void main(String[] args) throws Exception
{
BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
ArrayList<Integer> A = new ArrayList<>();
long rem = 1;
for (int i = 1; i < n; i++) {
if (gcd(i,n) == 1) {
A.add(i);
rem = (rem*i)%n;
}
}
int k = A.size();
System.out.println((rem == 1)?k:(k-1));
for (int i : A) {
if (i == 1 || i != rem) System.out.print(i+" ");
}
System.out.println();
}
static int gcd(int a, int b) {
return b == 0 ? a : gcd(b,a%b);
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 7f3cef3251bd1d149677738cc1653dd6 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int n = Integer.parseInt(br.readLine());
// sieve
int[] sieve = new int[n+1];
for (int i = 2; i <= n; i++)
{
if (sieve[i] == 0)
{
for (int j = i; j <= n; j += i)
{
sieve[j] = i;
}
}
}
int num = n;
boolean[] cannotUse = new boolean[n+1];
ArrayList<Integer> factors = new ArrayList<>();
while (num != 1)
{
factors.add(sieve[num]);
System.err.println(num + " " + sieve[num]);
num /= sieve[num];
}
for (int factor : factors)
{
for (int i = factor; i <= n; i += factor)
{
cannotUse[i] = true;
}
}
long product = 1;
ArrayList<Integer> ans = new ArrayList<>();
for (int i = 1; i < n; i++)
{
if (!cannotUse[i])
{
product *= i;
ans.add(i);
product %= n;
}
}
Collections.sort(ans);
if (product != 1)
ans.remove(Collections.binarySearch(ans, (int)product));
pw.println(ans.size());
for (int i = 0; i < ans.size(); i++)
{
if (i == ans.size() - 1)
pw.println(ans.get(i));
else pw.print(ans.get(i) + " ");
}
pw.close();
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 7263d5da4e940f3d818d827c1dc0fac7 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | //Utilities
import java.io.*;
import java.util.*;
public class a {
static int t;
static int n;
static int[] fac, inv;
static ArrayList<Integer> res;
static boolean[] vis;
public static void main(String[] args) throws IOException {
t = 1;
while (t-- > 0) {
n = in.iscan();
res = new ArrayList<Integer>();
long product = 1;
for (int i = 1; i < n; i++) {
if (UTILITIES.gcd(i, n) == 1) {
res.add(i);
product = product * i % n;
}
}
if (product == n-1) {
res.remove(res.get(res.size()-1));
product = 1;
}
if (res.isEmpty()) {
res.add(1);
}
out.println(res.size());
for (int x : res) {
out.print(x + " ");
}
out.println();
}
out.close();
}
static INPUT in = new INPUT(System.in);
static PrintWriter out = new PrintWriter(System.out);
private static class INPUT {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public INPUT (InputStream stream) {
this.stream = stream;
}
public INPUT (String file) throws IOException {
this.stream = new FileInputStream (file);
}
public int cscan () throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read (buf);
}
if (numChars == -1)
return numChars;
return buf[curChar++];
}
public int iscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
int res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public String sscan () throws IOException {
int c = cscan ();
while (space (c))
c = cscan ();
StringBuilder res = new StringBuilder ();
do {
res.appendCodePoint (c);
c = cscan ();
}
while (!space (c));
return res.toString ();
}
public double dscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
double res = 0;
while (!space (c) && c != '.') {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
res *= 10;
res += c - '0';
c = cscan ();
}
if (c == '.') {
c = cscan ();
double m = 1;
while (!space (c)) {
if (c == 'e' || c == 'E')
return res * UTILITIES.fast_pow (10, iscan ());
m /= 10;
res += (c - '0') * m;
c = cscan ();
}
}
return res * sgn;
}
public long lscan () throws IOException {
int c = cscan (), sgn = 1;
while (space (c))
c = cscan ();
if (c == '-') {
sgn = -1;
c = cscan ();
}
long res = 0;
do {
res = (res << 1) + (res << 3);
res += c - '0';
c = cscan ();
}
while (!space (c));
return res * sgn;
}
public boolean space (int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
public static class UTILITIES {
static final double EPS = 10e-6;
public static void sort(int[] a, boolean increasing) {
ArrayList<Integer> arr = new ArrayList<Integer>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(long[] a, boolean increasing) {
ArrayList<Long> arr = new ArrayList<Long>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static void sort(double[] a, boolean increasing) {
ArrayList<Double> arr = new ArrayList<Double>();
int n = a.length;
for (int i = 0; i < n; i++) {
arr.add(a[i]);
}
Collections.sort(arr);
for (int i = 0; i < n; i++) {
if (increasing) {
a[i] = arr.get(i);
}
else {
a[i] = arr.get(n-1-i);
}
}
}
public static int lower_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static int upper_bound (int[] arr, int x) {
int low = 0, high = arr.length, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > x)
high = mid;
else
low = mid + 1;
}
return low;
}
public static void updateMap(HashMap<Integer, Integer> map, int key, int v) {
if (!map.containsKey(key)) {
map.put(key, v);
}
else {
map.put(key, map.get(key) + v);
}
if (map.get(key) == 0) {
map.remove(key);
}
}
public static long gcd (long a, long b) {
return b == 0 ? a : gcd (b, a % b);
}
public static long lcm (long a, long b) {
return a * b / gcd (a, b);
}
public static long fast_pow_mod (long b, long x, int mod) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod;
return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod;
}
public static long fast_pow (long b, long x) {
if (x == 0) return 1;
if (x == 1) return b;
if (x % 2 == 0) return fast_pow (b * b, x / 2);
return b * fast_pow (b * b, x / 2);
}
public static long choose (long n, long k) {
k = Math.min (k, n - k);
long val = 1;
for (int i = 0; i < k; ++i)
val = val * (n - i) / (i + 1);
return val;
}
public static long permute (int n, int k) {
if (n < k) return 0;
long val = 1;
for (int i = 0; i < k; ++i)
val = (val * (n - i));
return val;
}
// start of permutation and lower/upper bound template
public static void nextPermutation(int[] nums) {
//find first decreasing digit
int mark = -1;
for (int i = nums.length - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
mark = i - 1;
break;
}
}
if (mark == -1) {
reverse(nums, 0, nums.length - 1);
return;
}
int idx = nums.length-1;
for (int i = nums.length-1; i >= mark+1; i--) {
if (nums[i] > nums[mark]) {
idx = i;
break;
}
}
swap(nums, mark, idx);
reverse(nums, mark + 1, nums.length - 1);
}
public static void swap(int[] nums, int i, int j) {
int t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
public static void reverse(int[] nums, int i, int j) {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
}
static int lower_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] >= cmp) high = mid;
else low = mid + 1;
}
return low;
}
static int upper_bound (int[] arr, int hi, int cmp) {
int low = 0, high = hi, mid = -1;
while (low < high) {
mid = (low + high) / 2;
if (arr[mid] > cmp) high = mid;
else low = mid + 1;
}
return low;
}
// end of permutation and lower/upper bound template
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 0822b074f5188a8a1bf0acbcbda3c09e | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class prod1modn {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
ArrayList<Integer> rp = new ArrayList<>();
for (int i = 1; i < n; i++) {
if (gcd(i, n) == 1) {
rp.add(i);
}
}
long[] pref = new long[rp.size()];
pref[0] = 1;
for (int i = 1; i < rp.size(); i++) {
pref[i] = (pref[i - 1] * rp.get(i)) % n;
}
int ans = 1;
for (int i = 0; i < rp.size(); i++) {
if (pref[i] == 1) {
ans = i + 1;
}
}
pw.println(ans);
for (int i = 0; i < ans; i++) {
pw.print(rp.get(i) + " ");
}
pw.close();
}
public static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 00b5c14845a802ed16a0a1b2c2462225 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | //package com.company;
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main{
static boolean[] primecheck;
static ArrayList<Integer>[] adj;
static int[] vis;
static int[] parent = new int[101];
static int[] rank = new int[101];
static int mod = (int)1e9 + 7;
public static void main(String[] args) {
OutputStream outputStream = System.out;
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(outputStream);
PROBLEM solver = new PROBLEM();
int t = 1;
//t = in.ni();
for (int i = 0; i < t; i++) {
//out.print("Case #" + (i+1) + ": ");
solver.solve(in, out);
}
out.close();
}
static class PROBLEM {
public void solve(FastReader in, PrintWriter out) {
int n = in.ni();
ArrayList<Integer> al = new ArrayList<>();
long ctr = 0, prod = 1;
for (int i = 1; i < n; i++) {
if(gcd(i,n) == 1){
al.add(i);
ctr++;
prod=(prod*i)%n;
}
}
if(prod != 1) ctr--;
out.println(ctr);
for(int i : al){
if(i != 1 && i == prod) continue;
out.print(i + " ");
}
}
}
static int find(int u){
if(u == parent[u]) return u;
return parent[u] = find(parent[u]);
}
static void union(int u, int v){
int a = find(u), b = find(v);
if(a == b) return;
if(rank[a] > rank[b]){
parent[b] = a;
rank[a] += rank[b];
}else{
parent[a] = b;
rank[b] += rank[a];
}
}
static void dsu(){
for (int i = 0; i < 101; i++) {
parent[i] = i;
rank[i] = 1;
}
}
static void dfs(int i, int ans){
vis[i] = 1;
for(int j : adj[i]) {
if (vis[j] == 0){
dfs(j, ans+1);
}
}
}
static boolean isPalindrome(char[] s){
boolean b = true;
for (int i = 0; i < s.length / 2; i++) {
if(s[i] != s[s.length-1-i]){
b = false;
break;
}
}
return b;
}
static void output(boolean b, PrintWriter out){
if(b) out.println("YES");
else out.println("NO");
}
static void pa(int[] a, PrintWriter out){
for (int j : a) {
out.print(j + " ");
}
out.println();
}
static void pa(long[] a, PrintWriter out){
for (long j : a) {
out.print(j + " ");
}
out.println();
}
public static void sortbyColumn(int arr[][], int col)
{
// Using built-in sort function Arrays.sort
Arrays.sort(arr, new Comparator<int[]>() {
@Override
// Compare values according to columns
public int compare(final int[] entry1,
final int[] entry2) {
// To sort in descending order revert
// the '>' Operator
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
}); // End of function call sort().
}
static boolean isPoT(long n){
return ((n&(n-1)) == 0);
}
static long sigmaK(long k){
return (k*(k+1))/2;
}
static void swap(int[] a, int l, int r) {
int temp = a[l];
a[l] = a[r];
a[r] = temp;
}
static int binarySearch(int[] a, int l, int r, int x){
if(r>=l){
int mid = l + (r-l)/2;
if(a[mid] == x) return mid;
if(a[mid] > x) return binarySearch(a, l, mid-1, x);
else return binarySearch(a,mid+1, r, x);
}
return -1;
}
static long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
static long lcm(long a, long b){
return (a / gcd(a, b)) * b;
}
static int ceil(int a, int b){
return (a+b-1)/b;
}
static long ceil(long a, long b){
return (a+b-1)/b;
}
static boolean isSquare(double a) {
boolean isSq = false;
double b = Math.sqrt(a);
double c = Math.sqrt(a) - Math.floor(b);
if (c == 0) isSq = true;
return isSq;
}
static long fast_pow(long a, long b) { //Jeel bhai OP
if(b == 0)
return 1L;
long val = fast_pow(a, b / 2);
if(b % 2 == 0)
return val * val % mod;
else
return val * val % mod * a % mod;
}
static int exponentMod(int A, int B, int C) {
// Base cases
if (A == 0)
return 0;
if (B == 0)
return 1;
// If B is even
long y;
if (B % 2 == 0) {
y = exponentMod(A, B / 2, C);
y = (y * y) % C;
}
// If B is odd
else {
y = A % C;
y = (y * exponentMod(A, B - 1, C) % C) % C;
}
return (int) ((y + C) % C);
}
// static class Pair implements Comparable<Pair>{
//
// int x;
// int y;
//
// Pair(int x, int y){
// this.x = x;
// this.y = y;
// }
//
// public int compareTo(Pair o){
//
// int ans = Integer.compare(x, o.x);
// if(o.x == x) ans = Integer.compare(y, o.y);
//
// return ans;
//
//// int ans = Integer.compare(y, o.y);
//// if(o.y == y) ans = Integer.compare(x, o.x);
////
//// return ans;
// }
// }
static class Tuple implements Comparable<Tuple>{
int x, y, id;
Tuple(int x, int y, int id){
this.x = x;
this.y = y;
this.id = id;
}
public int compareTo(Tuple o){
int ans = Integer.compare(x, o.x);
if(o.x == x) ans = Integer.compare(y, o.y);
return ans;
}
}
public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {
public U x;
public V y;
public Pair(U x, V y) {
this.x = x;
this.y = y;
}
public int hashCode() {
return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode());
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair<U, V> p = (Pair<U, V>) o;
return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y));
}
public int compareTo(Pair<U, V> b) {
int cmpU = x.compareTo(b.x);
return cmpU != 0 ? cmpU : y.compareTo(b.y);
}
public int compareToY(Pair<U, V> b) {
int cmpU = y.compareTo(b.y);
return cmpU != 0 ? cmpU : x.compareTo(b.x);
}
public String toString() {
return String.format("(%s, %s)", x.toString(), y.toString());
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() {
return Integer.parseInt(next());
}
long nl() {
return Long.parseLong(next());
}
double nd() {
return Double.parseDouble(next());
}
char nc() {
return next().charAt(0);
}
boolean nb() {
return !(ni() == 0);
}
// boolean nextBoolean(){return Boolean.parseBoolean(next());}
String nline() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int[] ra(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) array[i] = ni();
return array;
}
}
private static int[] mergeSort(int[] array) {
//array.length replaced with ctr
int ctr = array.length;
if (ctr <= 1) {
return array;
}
int midpoint = ctr / 2;
int[] left = new int[midpoint];
int[] right;
if (ctr % 2 == 0) {
right = new int[midpoint];
} else {
right = new int[midpoint + 1];
}
for (int i = 0; i < left.length; i++) {
left[i] = array[i];
}
for (int i = 0; i < right.length; i++) {
right[i] = array[i + midpoint];
}
left = mergeSort(left);
right = mergeSort(right);
int[] result = merge(left, right);
return result;
}
private static int[] merge(int[] left, int[] right) {
int[] result = new int[left.length + right.length];
int leftPointer = 0, rightPointer = 0, resultPointer = 0;
while (leftPointer < left.length || rightPointer < right.length) {
if (leftPointer < left.length && rightPointer < right.length) {
if (left[leftPointer] < right[rightPointer]) {
result[resultPointer++] = left[leftPointer++];
} else {
result[resultPointer++] = right[rightPointer++];
}
} else if (leftPointer < left.length) {
result[resultPointer++] = left[leftPointer++];
} else {
result[resultPointer++] = right[rightPointer++];
}
}
return result;
}
public static void Sieve(int n) {
Arrays.fill(primecheck, true);
primecheck[0] = false;
primecheck[1] = false;
for (int i = 2; i * i < n + 1; i++) {
if (primecheck[i]) {
for (int j = i * 2; j < n + 1; j += i) {
primecheck[j] = false;
}
}
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | e8f51d2fe51d9412ad6e8c32cd273a6a | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
public class Main {
static class Pair {
long l;
char c;
public Pair(long l,char c) {
this.l = l;
this.c=c;
}
public String toString() {
return l +" "+c;
}
}
static final int INF = 1 << 30;
static final long INFL = 1L << 60;
static final long NINF = INFL * -1;
static final long mod = (long) 1e9 + 7;
static final long mod2 = 998244353;
static DecimalFormat df = new DecimalFormat("0.00000000000000");
public static final double PI = 3.141592653589793d, eps = 1e-9;
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC
{
public static void solve(int testNumber, InputReader in, OutputWriter out)
{
int t1=1;
while (t1-->0)
{
long n=in.nextLong();
long num=1;
if(n<=4)
{
out.printLine(1);
out.printLine(1);
continue;
}
ArrayList<Long> al=new ArrayList<>();
al.add(1L);
for(long i=2;i<n;i++)
{
if(CP.gcd(i,n)==1)
{
al.add(i);
num=(num%n*i%n)%n;
}
}
if(num%n!=1)
{
al.remove(al.size()-1);
}
out.printLine(al.size());
for(int i=0;i<al.size();++i)
{
out.print(al.get(i)+" ");
}
out.printLine();
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int arraySize) {
int[] array = new int[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = readInt();
}
return array;
}
private int skip() {
int b;
while ((b = read()) != -1 && isSpaceChar(b)) ;
return b;
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = read();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n && !(isSpaceChar(b))) {
buf[p++] = (char) b;
b = read();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
public String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isNewLine(int c) {
return c == '\n';
}
public String nextLine() {
int c = read();
StringBuilder result = new StringBuilder();
do {
result.appendCodePoint(c);
c = read();
} while (!isNewLine(c));
return result.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sign = 1;
if (c == '-') {
sign = -1;
c = read();
}
long result = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
result *= 10;
result += c & 15;
c = read();
} while (!isSpaceChar(c));
return result * sign;
}
public long[] nextLongArray(int arraySize) {
long array[] = new long[arraySize];
for (int i = 0; i < arraySize; i++) {
array[i] = nextLong();
}
return array;
}
public double nextDouble() {
double ret = 0, div = 1;
byte c = (byte) read();
while (c <= ' ') {
c = (byte) read();
}
boolean neg = (c == '-');
if (neg) {
c = (byte) read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = (byte) read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = (byte) read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg) {
return -ret;
}
return ret;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static class CP {
static boolean isPrime(long n) {
if (n <= 1)
return false;
if (n == 2 || n == 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; (long) i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false;
}
return true;
}
public static int log2(int N) {
int result = (int) (Math.log(N) / Math.log(2));
return result;
}
public static long pow(long a, long n, long mod) {
// a %= mod;
long ret = 1;
int x = 63 - Long.numberOfLeadingZeros(n);
for (; x >= 0; x--) {
ret = ret * ret % mod;
if (n << 63 - x < 0)
ret = ret * a % mod;
}
return ret;
}
public static boolean isComposite(long n) {
if (n < 2)
return true;
if (n == 2 || n == 3)
return false;
if (n % 2 == 0 || n % 3 == 0)
return true;
for (long i = 6L; i * i <= n; i += 6)
if (n % (i - 1) == 0 || n % (i + 1) == 0)
return true;
return false;
}
static int ifnotPrime(int[] prime, int x) {
return (prime[x / 64] & (1 << ((x >> 1) & 31)));
}
static int log2(long n) {
return (int) (Math.log10(n) / Math.log10(2));
}
static void makeComposite(int[] prime, int x) {
prime[x / 64] |= (1 << ((x >> 1) & 31));
}
public static String swap(String a, int i, int j) {
char temp;
char[] charArray = a.toCharArray();
temp = charArray[i];
charArray[i] = charArray[j];
charArray[j] = temp;
return String.valueOf(charArray);
}
public static int[] sieveEratosthenes(int n) {
if (n <= 32) {
int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
for (int i = 0; i < primes.length; i++) {
if (n < primes[i]) {
return Arrays.copyOf(primes, i);
}
}
return primes;
}
int u = n + 32;
double lu = Math.log(u);
int[] ret = new int[(int) (u / lu + u / lu / lu * 1.5)];
ret[0] = 2;
int pos = 1;
int[] isnp = new int[(n + 1) / 32 / 2 + 1];
int sup = (n + 1) / 32 / 2 + 1;
int[] tprimes = {3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
for (int tp : tprimes) {
ret[pos++] = tp;
int[] ptn = new int[tp];
for (int i = (tp - 3) / 2; i < tp << 5; i += tp)
ptn[i >> 5] |= 1 << (i & 31);
for (int j = 0; j < sup; j += tp) {
for (int i = 0; i < tp && i + j < sup; i++) {
isnp[j + i] |= ptn[i];
}
}
}
// 3,5,7
// 2x+3=n
int[] magic = {0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4,
13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14};
int h = n / 2;
for (int i = 0; i < sup; i++) {
for (int j = ~isnp[i]; j != 0; j &= j - 1) {
int pp = i << 5 | magic[(j & -j) * 0x076be629 >>> 27];
int p = 2 * pp + 3;
if (p > n)
break;
ret[pos++] = p;
if ((long) p * p > n)
continue;
for (int q = (p * p - 3) / 2; q <= h; q += p)
isnp[q >> 5] |= 1 << q;
}
}
return Arrays.copyOf(ret, pos);
}
static int digit(long s) {
int brute = 0;
while (s > 0) {
s /= 10;
brute++;
}
return brute;
}
public static int[] primefacs(int n, int[] primes) {
int[] ret = new int[15];
int rp = 0;
for (int p : primes) {
if (p * p > n) break;
int i;
for (i = 0; n % p == 0; n /= p, i++) ;
if (i > 0) ret[rp++] = p;
}
if (n != 1) ret[rp++] = n;
return Arrays.copyOf(ret, rp);
}
static ArrayList<Integer> bitWiseSieve(int n) {
ArrayList<Integer> al = new ArrayList<>();
int prime[] = new int[n / 64 + 1];
for (int i = 3; i * i <= n; i += 2) {
if (ifnotPrime(prime, i) == 0)
for (int j = i * i, k = i << 1;
j < n; j += k)
makeComposite(prime, j);
}
al.add(2);
for (int i = 3; i <= n; i += 2)
if (ifnotPrime(prime, i) == 0)
al.add(i);
return al;
}
public static long[] sort(long arr[]) {
List<Long> list = new ArrayList<>();
for (long n : arr) {
list.add(n);
}
Collections.sort(list);
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
return arr;
}
public static long[] revsort(long[] arr) {
List<Long> list = new ArrayList<>();
for (long n : arr) {
list.add(n);
}
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
return arr;
}
public static int[] revsort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int n : arr) {
list.add(n);
}
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
return arr;
}
public static ArrayList<Integer> reverse(
ArrayList<Integer> data,
int left, int right)
{
// Reverse the sub-array
while (left < right)
{
int temp = data.get(left);
data.set(left++,
data.get(right));
data.set(right--, temp);
}
// Return the updated array
return data;
}
static ArrayList<Integer> sieve(long size) {
ArrayList<Integer> pr = new ArrayList<Integer>();
boolean prime[] = new boolean[(int) size];
for (int i = 2; i < prime.length; i++) prime[i] = true;
for (int i = 2; i * i < prime.length; i++) {
if (prime[i]) {
for (int j = i * i; j < prime.length; j += i) {
prime[j] = false;
}
}
}
for (int i = 2; i < prime.length; i++) if (prime[i]) pr.add(i);
return pr;
}
static ArrayList<Integer> segmented_sieve(int l, int r, ArrayList<Integer> primes) {
ArrayList<Integer> al = new ArrayList<>();
if (l == 1) ++l;
int max = r - l + 1;
int arr[] = new int[max];
for (int p : primes) {
if (p * p <= r) {
int i = (l / p) * p;
if (i < l) i += p;
for (; i <= r; i += p) {
if (i != p) {
arr[i - l] = 1;
}
}
}
}
for (int i = 0; i < max; ++i) {
if (arr[i] == 0) {
al.add(l + i);
}
}
return al;
}
static boolean isfPrime(long n, int iteration) {
if (n == 0 || n == 1)
return false;
if (n == 2)
return true;
if (n % 2 == 0)
return false;
Random rand = new Random();
for (int i = 0; i < iteration; i++) {
long r = Math.abs(rand.nextLong());
long a = r % (n - 1) + 1;
if (modPow(a, n - 1, n) != 1)
return false;
}
return true;
}
static long modPow(long a, long b, long c) {
long res = 1;
for (int i = 0; i < b; i++) {
res *= a;
res %= c;
}
return res % c;
}
private static long binPower(long a, long l, long mod) {
long res = 0;
while (l > 0) {
if ((l & 1) == 1) {
res = mulmod(res, a, mod);
l >>= 1;
}
a = mulmod(a, a, mod);
}
return res;
}
private static long mulmod(long a, long b, long c) {
long x = 0, y = a % c;
while (b > 0) {
if (b % 2 == 1) {
x = (x + y) % c;
}
y = (y * 2L) % c;
b /= 2;
}
return x % c;
}
static long binary_Expo(long a, long b) {
long res = 1;
while (b != 0) {
if ((b & 1) == 1) {
res *= a;
--b;
}
a *= a;
b /= 2;
}
return res;
}
static void swap(int a, int b) {
int tp = b;
b = a;
a = tp;
}
static long Modular_Expo(long a, long b, long mod) {
long res = 1;
while (b != 0) {
if ((b & 1) == 1) {
res = (res * a) % mod;
--b;
}
a = (a * a) % mod;
b /= 2;
}
return res % mod;
}
static int i_gcd(int a, int b) {
while (true) {
if (b == 0)
return a;
int c = a;
a = b;
b = c % b;
}
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long ceil_div(long a, long b) {
return (a + b - 1) / b;
}
static int getIthBitFromInt(int bits, int i) {
return (bits >> (i - 1)) & 1;
}
private static TreeMap<Long, Long> primeFactorize(long n) {
TreeMap<Long, Long> pf = new TreeMap<>(Collections.reverseOrder());
long cnt = 0;
long total = 1;
for (long i = 2; (long) i * i <= n; ++i) {
if (n % i == 0) {
cnt = 0;
while (n % i == 0) {
++cnt;
n /= i;
}
pf.put(cnt, i);
//total*=(cnt+1);
}
}
if (n > 1) {
pf.put(1L, n);
//total*=2;
}
return pf;
}
//less than or equal
private static int lower_bound(List<Integer> list, int val) {
int ans = -1, lo = 0, hi = list.size() - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(int[] arr, int val) {
int ans = -1, lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(long[] arr, long val) {
int ans = -1, lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
// greater than or equal
private static int upper_bound(List<Integer> list, int val) {
int ans = list.size(), lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(int[] arr, int val) {
int ans = arr.length, lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(long[] arr, long val) {
int ans = arr.length, lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
// ==================== LIS & LNDS ================================
private static int LIS(int arr[], int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find1(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find1(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) >= val) {
ret = mid;
j = mid - 1;
} else {
i = mid + 1;
}
}
return ret;
}
private static int LNDS(int[] arr, int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find2(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find2(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) <= val) {
i = mid + 1;
} else {
ret = mid;
j = mid - 1;
}
}
return ret;
}
private static long nCr(long n, long r) {
if (n - r > r)
r = n - r;
long ans = 1L;
for (long i = r + 1; i <= n; i++)
ans *= i;
for (long i = 2; i <= n - r; i++)
ans /= i;
return ans;
}
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
private static String sortString(String str) {
int[] arr = new int[256];
for (char ch : str.toCharArray())
arr[ch]++;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 256; i++)
while (arr[i]-- > 0)
sb.append((char) i);
return sb.toString();
}
static boolean isSquarefactor(int x, int factor, int target) {
int s = (int) Math.round(Math.sqrt(x));
return factor * s * s == target;
}
static boolean isSquare(int x) {
int s = (int) Math.round(Math.sqrt(x));
return x * x == s;
}
static int bs(ArrayList<Integer> al, int val) {
int l = 0, h = al.size() - 1, mid = 0, ans = -1;
while (l <= h) {
mid = (l + h) >> 1;
if (al.get(mid) == val) {
return mid;
} else if (al.get(mid) > val) {
h = mid - 1;
} else {
l = mid + 1;
}
}
return ans;
}
static void sort(int a[]) // heap sort
{
PriorityQueue<Integer> q = new PriorityQueue<>();
for (int i = 0; i < a.length; i++)
q.add(a[i]);
for (int i = 0; i < a.length; i++)
a[i] = q.poll();
}
static void shuffle(int[] in) {
for (int i = 0; i < in.length; i++) {
int idx = (int) (Math.random() * in.length);
fast_swap(in, idx, i);
}
}
public static int[] radixSort2(int[] a) {
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for (int v : a) {
c0[(v & 0xff) + 1]++;
c1[(v >>> 8 & 0xff) + 1]++;
c2[(v >>> 16 & 0xff) + 1]++;
c3[(v >>> 24 ^ 0x80) + 1]++;
}
for (int i = 0; i < 0xff; i++) {
c0[i + 1] += c0[i];
c1[i + 1] += c1[i];
c2[i + 1] += c2[i];
c3[i + 1] += c3[i];
}
int[] t = new int[n];
for (int v : a) t[c0[v & 0xff]++] = v;
for (int v : t) a[c1[v >>> 8 & 0xff]++] = v;
for (int v : a) t[c2[v >>> 16 & 0xff]++] = v;
for (int v : t) a[c3[v >>> 24 ^ 0x80]++] = v;
return a;
}
static int[] computeLps(String pat) {
int len = 0, i = 1, m = pat.length();
int lps[] = new int[m];
lps[0] = 0;
while (i < m) {
if (pat.charAt(i) == pat.charAt(len)) {
++len;
lps[i] = len;
++i;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = len;
++i;
}
}
}
return lps;
}
static ArrayList<Integer> kmp(String s, String pat) {
ArrayList<Integer> al = new ArrayList<>();
int n = s.length(), m = pat.length();
int lps[] = computeLps(pat);
int i = 0, j = 0;
while (i < n) {
if (s.charAt(i) == pat.charAt(j)) {
i++;
j++;
if (j == m) {
al.add(i - j);
j = lps[j - 1];
}
} else {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
return al;
}
static void reverse_ruffle_sort(int a[]) {
shuffle(a);
Arrays.sort(a);
for (int l = 0, r = a.length - 1; l < r; ++l, --r)
fast_swap(a, l, r);
}
static void ruffle_sort(int a[]) {
shuffle(a);
Arrays.sort(a);
}
static int getMax(int arr[], int n) {
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
static ArrayList<Long> primeFactors(long n) {
ArrayList<Long> al = new ArrayList<>();
al.add(1L);
while (n % 2 == 0) {
if (!al.contains(2L)) {
al.add(2L);
}
n /= 2L;
}
for (long i = 3; (long) i * i <= n; i += 2) {
while ((n % i == 0)) {
if (!al.contains((long) i)) {
al.add((long) i);
}
n /= i;
}
}
if (n > 2) {
if (!al.contains(n)) {
al.add(n);
}
}
return al;
}
public static long totFactors(long n) {
long cnt = 0, tot = 1;
while (n % 2 == 0) {
n /= 2;
++cnt;
}
tot *= (cnt + 1);
for (int i = 3; i <= Math.sqrt(n); i += 2) {
cnt = 0;
while (n % i == 0) {
n /= i;
++cnt;
}
tot *= (cnt + 1);
}
if (n > 2) {
tot *= 2;
}
return tot;
}
static int[] z_function(String s) {
int n = s.length(), z[] = new int[n];
for (int i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r)
z[i] = Math.min(z[i - l], r - i + 1);
while (i + z[i] < n && s.charAt(z[i]) == s.charAt(i + z[i]))
++z[i];
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
return z;
}
static void fast_swap(int[] a, int idx1, int idx2) {
if (a[idx1] == a[idx2])
return;
a[idx1] ^= a[idx2];
a[idx2] ^= a[idx1];
a[idx1] ^= a[idx2];
}
static void fast_swap(long[] a, int idx1, int idx2) {
if (a[idx1] == a[idx2])
return;
a[idx1] ^= a[idx2];
a[idx2] ^= a[idx1];
a[idx1] ^= a[idx2];
}
public static void fast_sort(long[] array) {
ArrayList<Long> copy = new ArrayList<>();
for (long i : array)
copy.add(i);
Collections.sort(copy);
for (int i = 0; i < array.length; i++)
array[i] = copy.get(i);
}
static int factorsCount(int n) {
boolean hash[] = new boolean[n + 1];
Arrays.fill(hash, true);
for (int p = 2; p * p < n; p++)
if (hash[p] == true)
for (int i = p * 2; i < n; i += p)
hash[i] = false;
int total = 1;
for (int p = 2; p <= n; p++) {
if (hash[p]) {
int count = 0;
if (n % p == 0) {
while (n % p == 0) {
n = n / p;
count++;
}
total = total * (count + 1);
}
}
}
return total;
}
static long binomialCoeff(long n, long k) {
long res = 1;
if (k > n - k)
k = n - k;
for (int i = 0; i < k; ++i) {
res = (res * (n - i));
res /= (i + 1);
}
return res;
}
static long nck(long fact[], long inv[], long n, long k) {
if (k > n) return 0;
long res = fact[(int) n];
res = (int) ((res * inv[(int) k]));
res = (int) ((res * inv[(int) (n - k)]));
return res % mod;
}
public static long fact(long x) {
long fact = 1;
for (int i = 2; i <= x; ++i) {
fact = fact * i;
}
return fact;
}
public static ArrayList<Long> getFact(long x) {
ArrayList<Long> facts = new ArrayList<>();
for (long i = 2; i * i <= x; ++i) {
if (x % i == 0) {
facts.add(i);
if (i != x / i) {
facts.add(x / i);
}
}
}
return facts;
}
static void matrix_ex(long n, long[][] A, long[][] I) {
while (n > 0) {
if (n % 2 == 0) {
Multiply(A, A);
n /= 2;
} else {
Multiply(I, A);
n--;
}
}
}
static void Multiply(long[][] A, long[][] B) {
int n = A.length, m = A[0].length, p = B[0].length;
long[][] C = new long[n][p];
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
for (int k = 0; k < m; k++) {
C[i][j] += ((A[i][k] % mod) * (B[k][j] % mod)) % mod;
C[i][j] = C[i][j] % mod;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < p; j++) {
A[i][j] = C[i][j];
}
}
}
public static HashMap<Character, Integer> sortMapDesc(HashMap<Character, Integer> map) {
List<Map.Entry<Character, Integer>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, (o1, o2) -> o2.getValue() - o1.getValue());
HashMap<Character, Integer> temp = new LinkedHashMap<>();
for (Map.Entry<Character, Integer> i : list) {
temp.put(i.getKey(), i.getValue());
}
return temp;
}
public static HashMap<Integer, Integer> sortMapAsc(HashMap<Integer, Integer> map) {
List<Map.Entry<Integer, Integer>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, (o1, o2) -> o1.getValue() - o2.getValue());
HashMap<Integer, Integer> temp = new LinkedHashMap<>();
for (Map.Entry<Integer, Integer> i : list) {
temp.put(i.getKey(), i.getValue());
}
return temp;
}
public static long lcm(long l, long l2) {
long val = gcd(l, l2);
return (l * l2) / val;
}
public static int isSubsequence(String s, String t) {
int n = s.length();
int m = t.length();
if (m > n) {
return Integer.MAX_VALUE;
}
int i = 0, j = 0, skip = 0;
while (i < n && j < m) {
if (s.charAt(i) == t.charAt(j)) {
--skip;
++j;
}
++skip;
++i;
}
while (i < n) {
++i;
++skip;
}
if (j != m) {
skip = Integer.MAX_VALUE;
}
return skip;
}
static class TreeMultiSet<T> implements Iterable<T> {
private final TreeMap<T, Integer> map;
private int size;
public TreeMultiSet() {
map = new TreeMap<>();
size = 0;
}
public TreeMultiSet(boolean reverse) {
if (reverse) map = new TreeMap<>(Collections.reverseOrder());
else map = new TreeMap<>();
size = 0;
}
public void clear() {
map.clear();
size = 0;
}
public int size() {
return size;
}
public int setSize() {
return map.size();
}
public boolean contains(T a) {
return map.containsKey(a);
}
public boolean isEmpty() {
return size == 0;
}
public Integer get(T a) {
return map.getOrDefault(a, 0);
}
public void add(T a, int count) {
int cur = get(a);
map.put(a, cur + count);
size += count;
if (cur + count == 0) map.remove(a);
}
public void addOne(T a) {
add(a, 1);
}
public void remove(T a, int count) {
add(a, Math.max(-get(a), -count));
}
public void removeOne(T a) {
remove(a, 1);
}
public void removeAll(T a) {
remove(a, Integer.MAX_VALUE - 10);
}
public T ceiling(T a) {
return map.ceilingKey(a);
}
public T floor(T a) {
return map.floorKey(a);
}
public T first() {
return map.firstKey();
}
public T last() {
return map.lastKey();
}
public T higher(T a) {
return map.higherKey(a);
}
public T lower(T a) {
return map.lowerKey(a);
}
public T pollFirst() {
T a = first();
removeOne(a);
return a;
}
public T pollLast() {
T a = last();
removeOne(a);
return a;
}
public Iterator<T> iterator() {
return new Iterator<T>() {
private final Iterator<T> iter = map.keySet().iterator();
private int count = 0;
private T curElement;
public boolean hasNext() {
return iter.hasNext() || count > 0;
}
public T next() {
if (count == 0) {
curElement = iter.next();
count = get(curElement);
}
count--;
return curElement;
}
};
}
}
public static long[][] combination(int l, int r) {
long[][] pascal = new long[l + 1][r + 1];
pascal[0][0] = 1;
for (int i = 1; i <= l; ++i) {
pascal[i][0] = 1;
for (int j = 1; j <= r; ++j) {
pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
}
}
return pascal;
}
public static long gcd(long... array) {
long ret = array[0];
for (int i = 1; i < array.length; ++i) ret = gcd(ret, array[i]);
return ret;
}
public static long lcm(int... array) {
long ret = array[0];
for (int i = 1; i < array.length; ++i) ret = lcm(ret, array[i]);
return ret;
}
public static int min(int a, int b) {
return a < b ? a : b;
}
public static int min(int... array) {
int ret = array[0];
for (int i = 1; i < array.length; ++i) ret = min(ret, array[i]);
return ret;
}
public static long min(long a, long b) {
return a < b ? a : b;
}
public static long min(long... array) {
long ret = array[0];
for (int i = 1; i < array.length; ++i) ret = min(ret, array[i]);
return ret;
}
public static int max(int a, int b) {
return a > b ? a : b;
}
public static int max(int... array) {
int ret = array[0];
for (int i = 1; i < array.length; ++i) ret = max(ret, array[i]);
return ret;
}
public static long max(long a, long b) {
return a > b ? a : b;
}
public static long max(long... array) {
long ret = array[0];
for (int i = 1; i < array.length; ++i) ret = max(ret, array[i]);
return ret;
}
public static long sum(int... array) {
long ret = 0;
for (int i : array) ret += i;
return ret;
}
public static long sum(long... array) {
long ret = 0;
for (long i : array) ret += i;
return ret;
}
public static int modinv(long x, long mod) {
return (int) (CP.Modular_Expo(x, mod - 2, mod) % mod);
}
public static int lcs(String s, String t) {
int n = s.length(), m = t.length();
int dp[][] = new int[n + 1][m + 1];
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= m; ++j) {
if (i == 0 || j == 0) {
dp[i][j] = 0;
} else if (s.charAt(i - 1) == t.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[n][m];
}
}
//segment tree
public static class SegmentTree {
int n;
int[] arr, tree, lazy;
SegmentTree(int arr[]) {
this.arr = arr;
this.n = arr.length;
this.tree = new int[(n << 2)];
this.lazy = new int[(n << 2)];
build(1, 0, n - 1);
}
void build(int id, int start, int end) {
if (start == end)
tree[id] = arr[start];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
build(left, start, mid);
build(right, mid + 1, end);
tree[id] = tree[left] + tree[right];
}
}
void update(int l, int r, int val) {
update(1, 0, n - 1, l, r, val);
}
void update(int id, int start, int end, int l, int r, int val) {
distribute(id, start, end);
if (end < l || r < start)
return;
if (start == end)
tree[id] += val;
else if (l <= start && end <= r) {
lazy[id] += val;
distribute(id, start, end);
} else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
update(left, start, mid, l, r, val);
update(right, mid + 1, end, l, r, val);
tree[id] = tree[left] + tree[right];
}
}
int query(int l, int r) {
return query(1, 0, n - 1, l, r);
}
int query(int id, int start, int end, int l, int r) {
if (end < l || r < start)
return 0;
distribute(id, start, end);
if (start == end)
return tree[id];
else if (l <= start && end <= r)
return tree[id];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r);
}
}
void distribute(int id, int start, int end) {
if (start == end)
tree[id] += lazy[id];
else {
tree[id] += lazy[id] * (end - start + 1);
lazy[(id << 1)] += lazy[id];
lazy[(id << 1) + 1] += lazy[id];
}
lazy[id] = 0;
}
}
//TRIE
static class Trie {
class Node {
Node[] children;
boolean isEnd;
Node() {
children = new Node[26];
}
}
Node root;
Trie() {
root = new Node();
}
public void insert(String word) {
Node curr = root;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
curr.children[ch - 'a'] = new Node();
curr = curr.children[ch - 'a'];
}
curr.isEnd = true;
}
public boolean find(String word) {
Node curr = root;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
return false;
curr = curr.children[ch - 'a'];
}
return curr.isEnd;
}
}
//fenwick tree
static class FT {
long[] tree;
int n;
FT(int n) {
this.n = n;
this.tree = new long[n + 1];
}
FT(int[] arr, int n) {
this.n = n;
this.tree = new long[n + 1];
for (int i = 1; i <= n; i++) {
update(i, arr[i - 1]);
}
}
void update(int idx, int val) {
while (idx <= n) {
tree[idx] += val;
idx += idx & -idx;
}
}
long query(int l, int r) {
return getSum(r) - getSum(l - 1);
}
long getSum(int idx) {
long ans = 0L;
while (idx > 0) {
ans += tree[idx];
idx -= idx & -idx;
}
return ans;
}
}
//binary indexed tree
static class BIT {
long[][] tree;
int n, m;
BIT(int[][] mat, int n, int m) {
this.n = n;
this.m = m;
tree = new long[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
update(i, j, mat[i - 1][j - 1]);
}
}
}
void update(int x, int y, int val) {
while (x <= n) {
int t = y;
while (t <= m) {
tree[x][t] += val;
t += t & -t;
}
x += x & -x;
}
}
long query(int x1, int y1, int x2, int y2) {
return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1);
}
long getSum(int x, int y) {
long ans = 0L;
while (x > 0) {
int t = y;
while (t > 0) {
ans += tree[x][t];
t -= t & -t;
}
x -= x & -x;
}
return ans;
}
}
static class BSTCustom<T extends Comparable<? super T>> {
private Node<T> __root;
private Node<T> __tempnode;
private Node<T> __tempnode1;
private long __size;
private boolean __multi;
public BSTCustom() {
__root = null;
__size = 0;
__multi = false;
}
public BSTCustom(boolean __multi) {
__root = null;
__size = 0;
this.__multi = __multi;
}
public long lower_bound(T data) {
__tempnode = __root;
long index = 0;
while (__tempnode != null) {
int _comparator = data.compareTo(__tempnode.data);
if (_comparator < 0) {
__tempnode = __tempnode._left;
} else if (_comparator > 0) {
index += (__tempnode.count + __tempnode._leftside);
__tempnode = __tempnode._right;
} else {
index += (__tempnode._leftside);
break;
}
}
return index;
}
private Node<T> remove(Node<T> temp, T data, long count) {
if (temp == null) return temp;
int _comparator = data.compareTo(temp.data);
if (_comparator < 0) temp._left = remove(temp._left, data, count);
else if (_comparator > 0) temp._right = remove(temp._right, data, count);
else {
if (__multi && count < temp.count && temp.count > 1) {
__size -= count;
temp.count -= count;
} else {
__size -= temp.count;
if (temp._left == null && temp._right == null) return null;
else if (temp._left == null) return temp._right;
else if (temp._right == null) return temp._left;
else {
__tempnode = minValue(temp._right);
temp.data = __tempnode.data;
temp.count = __tempnode.count;
temp._right = remove(temp._right, __tempnode.data, __tempnode.count);
__size += temp.count;
}
}
}
// for __leftside and _rightside count
if (temp._left != null)
temp._leftside = temp._left._leftside + temp._left._rightside + temp._left.count;
else temp._leftside = 0;
if (temp._right != null)
temp._rightside = temp._right._leftside + temp._right._rightside + temp._right.count;
else temp._rightside = 0;
temp._height = Math.max(getheight(temp._left), getheight(temp._right)) + 1;
//Balancing
long diff = getDiff(temp);
if (diff > 1) {
if (getDiff(temp._left) >= 0) {
temp = rightRotate(temp);
} else {
temp._left = leftRotate(temp._left);
temp = rightRotate(temp);
}
} else if (diff < -1) {
if (getDiff(temp._right) <= 0) {
temp = leftRotate(temp);
} else {
temp._right = rightRotate(temp._right);
temp = leftRotate(temp);
}
}
return temp;
}
public T get(long index) {
__tempnode = __root;
long current = 0;
while (__tempnode != null) {
if (__tempnode._left == null) {
if (__tempnode.count + current > index) return __tempnode.data;
else {
current += __tempnode.count;
__tempnode = __tempnode._right;
}
} else {
if (current + __tempnode._leftside > index) __tempnode = __tempnode._left;
else if (current + __tempnode._leftside + __tempnode.count > index)
return __tempnode.data;
else {
current += __tempnode.count + __tempnode._leftside;
__tempnode = __tempnode._right;
}
}
}
return null;
}
private Node<T> minValue(Node<T> temp) {
__tempnode = temp;
while (__tempnode._left != null) __tempnode = __tempnode._left;
return __tempnode;
}
public void insert(T data, long... count) {
if (count.length == 0) __root = insert(__root, data, 1);
else if (count[0] > 0) __root = insert(__root, data, count[0]);
}
public void remove(T data, long... count) {
if (count.length == 0) __root = remove(__root, data, 1);
else if (count[0] > 0) __root = remove(__root, data, count[0]);
}
private Node<T> insert(Node<T> temp, T data, long count) {
if (temp == null) {
if (__multi) {
__size += count;
return new Node<>(data, count);
} else {
__size++;
return new Node<>(data);
}
}
int _comparator = data.compareTo(temp.data);
if (_comparator < 0) temp._left = insert(temp._left, data, count);
else if (_comparator > 0) temp._right = insert(temp._right, data, count);
else if (__multi) {
__size += count;
temp.count += count;
}
// for __leftside and _rightside count
if (temp._left != null)
temp._leftside = temp._left._leftside + temp._left._rightside + temp._left.count;
else temp._leftside = 0;
if (temp._right != null)
temp._rightside = temp._right._leftside + temp._right._rightside + temp._right.count;
else temp._rightside = 0;
temp._height = Math.max(getheight(temp._left), getheight(temp._right)) + 1;
//Balancing
long diff = getDiff(temp);
if (diff > 1) {
if (data.compareTo(temp._left.data) < 0) {
temp = rightRotate(temp);
} else if (data.compareTo(temp._left.data) > 0) {
temp._left = leftRotate(temp._left);
temp = rightRotate(temp);
}
} else if (diff < -1) {
if (data.compareTo(temp._right.data) > 0) {
temp = leftRotate(temp);
} else if (data.compareTo(temp._right.data) < 0) {
temp._right = rightRotate(temp._right);
temp = leftRotate(temp);
}
}
return temp;
}
private Node<T> rightRotate(Node<T> temp) {
__tempnode = temp._left;
__tempnode1 = __tempnode._right;
__tempnode._right = temp;
temp._left = __tempnode1;
//height updation
temp._height = Math.max(getheight(temp._left), getheight(temp._right)) + 1;
__tempnode._height = Math.max(getheight(__tempnode._left), getheight(__tempnode._right)) + 1;
//count updation
if (temp._left != null)
temp._leftside = temp._left._leftside + temp._left._rightside + temp._left.count;
else temp._leftside = 0;
if (temp._right != null)
temp._rightside = temp._right._leftside + temp._right._rightside + temp._right.count;
else temp._rightside = 0;
if (__tempnode._left != null)
__tempnode._leftside = __tempnode._left._leftside + __tempnode._left._rightside + __tempnode._left.count;
else __tempnode._leftside = 0;
if (__tempnode._right != null)
__tempnode._rightside = __tempnode._right._leftside + __tempnode._right._rightside + __tempnode._right.count;
else __tempnode._rightside = 0;
return __tempnode;
}
private Node<T> leftRotate(Node<T> temp) {
__tempnode = temp._right;
__tempnode1 = __tempnode._left;
__tempnode._left = temp;
temp._right = __tempnode1;
//height updation
temp._height = Math.max(getheight(temp._left), getheight(temp._right)) + 1;
__tempnode._height = Math.max(getheight(__tempnode._left), getheight(__tempnode._right)) + 1;
//count updation
if (temp._left != null)
temp._leftside = temp._left._leftside + temp._left._rightside + temp._left.count;
else temp._leftside = 0;
if (temp._right != null)
temp._rightside = temp._right._leftside + temp._right._rightside + temp._right.count;
else temp._rightside = 0;
if (__tempnode._left != null)
__tempnode._leftside = __tempnode._left._leftside + __tempnode._left._rightside + __tempnode._left.count;
else __tempnode._leftside = 0;
if (__tempnode._right != null)
__tempnode._rightside = __tempnode._right._leftside + __tempnode._right._rightside + __tempnode._right.count;
else __tempnode._rightside = 0;
return __tempnode;
}
private long getDiff(Node<T> temp) {
if (temp == null) return 0;
return getheight(temp._left) - getheight(temp._right);
}
public long getheight(Node<T> temp) {
if (temp == null) return 0;
return temp._height;
}
public long size() {
return this.__size;
}
private class Node<T> {
T data;
Node<T> _left;
Node<T> _right;
long _leftside;
long _rightside;
long _height;
long count;
public Node(T data) {
this.data = data;
this._left = null;
this._right = null;
this._leftside = 0;
this._rightside = 0;
this._height = 1;
this.count = 1;
}
public Node(T data, long count) {
this.data = data;
this._left = null;
this._right = null;
this._leftside = 0;
this._rightside = 0;
this._height = 1;
this.count = count;
}
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void printLine(int[] array) {
print(array);
writer.println();
}
public void close() {
writer.close();
}
public void printLine(long i) {
writer.println(i);
}
public void printLine(int i) {
writer.println(i);
}
public void printLine(char c) {
writer.println(c);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
void flush() {
writer.flush();
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 8d19ed8e14d248a36005f3ba05ee3d5b | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | /*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import javax.management.InstanceNotFoundException;
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = 1;
for (int t = 0; t < test; t++) {
solve();
}
out.close();
}
private static void solve() {
int n = sc.nextInt();
long curr = 1;
List<Integer> subseq = new ArrayList<>();
subseq.add(1);
for (int i = 2; i < n; i++) {
if (gcd(i, n) == 1) {
subseq.add(i);
curr *= i;
curr %= n;
}
}
int length = subseq.size();
if (curr != 1) {
length--;
}
out.println(length);
for (int i = 0; i < length; i++) {
out.print(subseq.get(i) + " ");
}
out.println();
}
private static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b,a % b);
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException end)
{
end.printStackTrace();
}
}
return str.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException end)
{
end.printStackTrace();
}
return str;
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 903e6b7a84d39b42bf79ddea116aa9ee | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class product1 {
static class FastIO extends PrintWriter {
private InputStream stream;
private byte[] buf = new byte[1 << 16];
private int curChar;
private int numChars;
// standard input
public FastIO() { this(System.in, System.out); }
public FastIO(InputStream i, OutputStream o) {
super(o);
stream = i;
}
// file input
public FastIO(String i, String o) throws IOException {
super(new FileWriter(o));
stream = new FileInputStream(i);
}
// throws InputMismatchException() if previously detected end of file
private int nextByte() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars == -1) {
return -1; // end of file
}
}
return buf[curChar++];
}
// to read in entire lines, replace c <= ' '
// with a function that checks whether c is a line break
public String next() {
int c;
do {
c = nextByte();
} while (c <= ' ');
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = nextByte();
} while (c > ' ');
return res.toString();
}
public int nextInt() { // nextLong() would be implemented similarly
int c;
do {
c = nextByte();
} while (c <= ' ');
int sgn = 1;
if (c == '-') {
sgn = -1;
c = nextByte();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res = 10 * res + c - '0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public double nextDouble() { return Double.parseDouble(next()); }
}
public static void main(String[] args){
FastIO io = new FastIO();
int x = io.nextInt();
boolean[] elements = new boolean[x];
ArrayList<Integer> primes = primeDivisors(x);
for(int p: primes){
for(int i = 0; i < x; i += p){
elements[i] = true;
}
}
long product = 1;
int count = 0;
for(int i = 1; i < x; i++){
if(!elements[i]){
product *= i;
product %= x;
count++;
}
}
if(product != 1){
elements[(int) product] = true;
count--;
}
io.println(count);
for(int i = 1; i < x; i++){
if(!elements[i]){
io.print(i + " ");
}
}
io.close();
}
static ArrayList<Integer> primeDivisors(int x){
ArrayList<Integer> primeList = new ArrayList<>();
int y = x;
for(int i = 2; i * i <= y; i++){
if(y % i == 0){
primeList.add(i);
while(y % i == 0){
y /= i;
}
}
}
if(y > 1){
primeList.add(y);
}
return primeList;
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 8029c548ad2d889f9e2b7d76a1dcc44c | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | //some updates in import stuff
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
//update this bad boi too
public class Main{
static int mod = (int) (Math.pow(10, 9)+7);
static final int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
static final int[] dx8 = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy8 = { -1, 0, 1, -1, 1, -1, 0, 1 };
static final int[] dx9 = { -1, -1, -1, 0, 0, 0, 1, 1, 1 }, dy9 = { -1, 0, 1, -1, 0, 1, -1, 0, 1 };
static final double eps = 1e-10;
static List<Integer> primeNumbers = new ArrayList<>();
public static void main(String[] args) throws IOException{
MyScanner sc = new MyScanner();
//changes in this line of code
out = new PrintWriter(new BufferedOutputStream(System.out));
// out = new PrintWriter(new BufferedWriter(new FileWriter("output.out")));
//WRITE YOUR CODE IN HERE
long n = sc.nextInt();
long product = 1;
ArrayList<Long> ans = new ArrayList<>();
for(long i = 1 ; i <= n; i++){
if(gcd(i, n) == 1){
ans.add(i);
product = (product * i)%n;
}
}
if(product % n != 1){
ans.remove(ans.size()-1);
}
out.println(ans.size());
for(long i : ans){
out.print(i + " ");
}
out.println();
out.close();
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//Updation Required
//Fenwick Tree (customisable)
//Segment Tree (customisable)
//-----CURRENTLY PRESENT-------//
//Graph
//DSU
//powerMODe
//power
//Segment Tree (work on this one)
//Prime Sieve
//Count Divisors
//Next Permutation
//Get NCR
//isVowel
//Sort (int)
//Sort (long)
//Binomial Coefficient
//Pair
//Triplet
//lcm (int & long)
//gcd (int & long)
//gcd (for binomial coefficient)
//swap (int & char)
//reverse
//Fast input and output
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//GRAPH (basic structure)
public static class Graph{
public int V;
public ArrayList<ArrayList<Integer>> edges;
//2 -> [0,1,2] (current)
Graph(int V){
this.V = V;
edges = new ArrayList<>(V+1);
for(int i= 0; i <= V; i++){
edges.add(new ArrayList<>());
}
}
public void addEdge(int from , int to){
edges.get(from).add(to);
}
}
//DSU (path and rank optimised)
public static class DisjointUnionSets {
int[] rank, parent;
int n;
public DisjointUnionSets(int n)
{
rank = new int[n];
parent = new int[n];
Arrays.fill(rank, 1);
Arrays.fill(parent,-1);
this.n = n;
}
public int find(int curr){
if(parent[curr] == -1)
return curr;
//path compression optimisation
return parent[curr] = find(parent[curr]);
}
public void union(int a, int b){
int s1 = find(a);
int s2 = find(b);
if(s1 != s2){
if(rank[s1] < rank[s2]){
parent[s1] = s2;
rank[s2] += rank[s1];
}else{
parent[s2] = s1;
rank[s1] += rank[s2];
}
}
}
}
//with mod
public static long powerMOD(long x, long y)
{
long res = 1L;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0){
x %= mod;
res %= mod;
res = (res * x)%mod;
}
// y must be even now
y = y >> 1; // y = y/2
x%= mod;
x = (x * x)%mod; // Change x to x^2
}
return res%mod;
}
//without mod
public static long power(long x, long y)
{
long res = 1L;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0){
res = (res * x);
}
// y must be even now
y = y >> 1; // y = y/
x = (x * x);
}
return res;
}
public static class segmentTree{
public long[] arr;
public long[] tree;
public long[] lazy;
segmentTree(long[] array){
int n = array.length;
arr = new long[n];
for(int i= 0; i < n; i++) arr[i] = array[i];
tree = new long[4*n + 1];
lazy = new long[4*n + 1];
}
public void build(int[]arr, int s, int e, int[] tree, int index){
if(s == e){
tree[index] = arr[s];
return;
}
//otherwise divide in two parts and fill both sides simply
int mid = (s+e)/2;
build(arr, s, mid, tree, 2*index);
build(arr, mid+1, e, tree, 2*index+1);
//who will build the current position dude
tree[index] = Math.min(tree[2*index], tree[2*index+1]);
}
public int query(int sr, int er, int sc, int ec, int index, int[] tree){
if(lazy[index] != 0){
tree[index] += lazy[index];
if(sc != ec){
lazy[2*index+1] += lazy[index];
lazy[2*index] += lazy[index];
}
lazy[index] = 0;
}
//no overlap
if(sr > ec || sc > er) return Integer.MAX_VALUE;
//found the index baby
if(sr <= sc && ec <= er) return tree[index];
//finding the index on both sides hehehehhe
int mid = (sc + ec)/2;
int left = query(sr, er, sc, mid, 2*index, tree);
int right = query(sr, er, mid+1, ec, 2*index + 1, tree);
return Integer.min(left, right);
}
//now we will do point update implementation
//it should be simple then we expected for sure
public void update(int index, int indexr, int increment, int[] tree, int s, int e){
if(lazy[index] != 0){
tree[index] += lazy[index];
if(s != e){
lazy[2*index+1] = lazy[index];
lazy[2*index] = lazy[index];
}
lazy[index] = 0;
}
//no overlap
if(indexr < s || indexr > e) return;
//found the required index
if(s == e){
tree[index] += increment;
return;
}
//search for the index on both sides
int mid = (s+e)/2;
update(2*index, indexr, increment, tree, s, mid);
update(2*index+1, indexr, increment, tree, mid+1, e);
//now update the current range simply
tree[index] = Math.min(tree[2*index+1], tree[2*index]);
}
public void rangeUpdate(int[] tree , int index, int s, int e, int sr, int er, int increment){
//if not at all in the same range
if(e < sr || er < s) return;
//complete then also move forward
if(s == e){
tree[index] += increment;
return;
}
//otherwise move in both subparts
int mid = (s+e)/2;
rangeUpdate(tree, 2*index, s, mid, sr, er, increment);
rangeUpdate(tree, 2*index + 1, mid+1, e, sr, er, increment);
//update current range too na
//i always forget this step for some reasons hehehe, idiot
tree[index] = Math.min(tree[2*index], tree[2*index + 1]);
}
public void rangeUpdateLazy(int[] tree, int index, int s, int e, int sr, int er, int increment){
//update lazy values
//resolve lazy value before going down
if(lazy[index] != 0){
tree[index] += lazy[index];
if(s != e){
lazy[2*index+1] += lazy[index];
lazy[2*index] += lazy[index];
}
lazy[index] = 0;
}
//no overlap case
if(sr > e || s > er) return;
//complete overlap
if(sr <= s && er >= e){
tree[index] += increment;
if(s != e){
lazy[2*index+1] += increment;
lazy[2*index] += increment;
}
return;
}
//otherwise go on both left and right side and do your shit
int mid = (s + e)/2;
rangeUpdateLazy(tree, 2*index, s, mid, sr, er, increment);
rangeUpdateLazy(tree, 2*index + 1, mid+1, e, sr, er, increment);
tree[index] = Math.min(tree[2*index+1], tree[2*index]);
return;
}
}
//prime sieve
public static void primeSieve(int n){
BitSet bitset = new BitSet(n+1);
for(long i = 0; i < n ; i++){
if (i == 0 || i == 1) {
bitset.set((int) i);
continue;
}
if(bitset.get((int) i)) continue;
primeNumbers.add((int)i);
for(long j = i; j <= n ; j+= i)
bitset.set((int)j);
}
}
//number of divisors
public static int countDivisors(long number){
if(number == 1) return 1;
List<Integer> primeFactors = new ArrayList<>();
int index = 0;
long curr = primeNumbers.get(index);
while(curr * curr <= number){
while(number % curr == 0){
number = number/curr;
primeFactors.add((int) curr);
}
index++;
curr = primeNumbers.get(index);
}
if(number != 1) primeFactors.add((int) number);
int current = primeFactors.get(0);
int totalDivisors = 1;
int currentCount = 2;
for (int i = 1; i < primeFactors.size(); i++) {
if (primeFactors.get(i) == current) {
currentCount++;
} else {
totalDivisors *= currentCount;
currentCount = 2;
current = primeFactors.get(i);
}
}
totalDivisors *= currentCount;
return totalDivisors;
}
//now adding next permutation function to java hehe
public static boolean next_permutation(int[] p) {
for (int a = p.length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for (int b = p.length - 1;; --b)
if (p[b] > p[a]) {
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true;
}
return false;
}
//finding the value of NCR in O(RlogN) time and O(1) space
public static long getNcR(int n, int r)
{
long p = 1, k = 1;
if (n - r < r) r = n - r;
if (r != 0) {
while (r > 0) {
p *= n;
k *= r;
long m = __gcd(p, k);
p /= m;
k /= m;
n--;
r--;
}
}
else {
p = 1;
}
return p;
}
//is vowel function
public static boolean isVowel(char c)
{
return (c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' || c=='u' || c=='U');
}
//to sort the array with better method
public static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
//sort long
public static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
//for calculating binomialCoeff
public static int binomialCoeff(int n, int k)
{
int C[] = new int[k + 1];
// nC0 is 1
C[0] = 1;
for (int i = 1; i <= n; i++) {
// Compute next row of pascal
// triangle using the previous row
for (int j = Math.min(i, k); j > 0; j--)
C[j] = C[j] + C[j - 1];
}
return C[k];
}
//Pair with int int
public static class Pair{
public int a;
public int b;
Pair(int a , int b){
this.a = a;
this.b = b;
}
@Override
public String toString(){
return a + " -> " + b;
}
}
//Triplet with int int int
public static class Triplet{
public int a;
public int b;
public int c;
Triplet(int a , int b, int c){
this.a = a;
this.b = b;
this.c = c;
}
@Override
public String toString(){
return a + " -> " + b;
}
}
//Shortcut function
public static long lcm(long a , long b){
return a * (b/gcd(a,b));
}
//let's make one for calculating lcm basically
public static int lcm(int a , int b){
return (a * b)/gcd(a,b);
}
//int version for gcd
public static int gcd(int a, int b){
if(b == 0)
return a;
return gcd(b , a%b);
}
//long version for gcd
public static long gcd(long a, long b){
if(b == 0)
return a;
return gcd(b , a%b);
}
//for ncr calculator(ignore this code)
public static long __gcd(long n1, long n2)
{
long gcd = 1;
for (int i = 1; i <= n1 && i <= n2; ++i) {
// Checks if i is factor of both integers
if (n1 % i == 0 && n2 % i == 0) {
gcd = i;
}
}
return gcd;
}
//swapping two elements in an array
public static void swap(int[] arr, int left , int right){
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
//for char array
public static void swap(char[] arr, int left , int right){
char temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
//reversing an array
public static void reverse(int[] arr){
int left = 0;
int right = arr.length-1;
while(left <= right){
swap(arr, left,right);
left++;
right--;
}
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
// public MyScanner() throws FileNotFoundException {
// br = new BufferedReader(new FileReader("input.in"));
// }
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//--------------------------------------------------------
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 8cb8f83d4356bc5a2b25ed0de027d4a8 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | // \(≧▽≦)/
import java.io.*;
import java.util.*;
public class tank {
static final FastScanner fs = new FastScanner();
//static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
int t = 1;
while(t-- > 0) {
run_case();
}
//out.close();
}
static void run_case() {
int n = fs.nextInt(), an = 0;
if(n == 2) {
System.out.println(1 + "\n" + 1);
return;
}
ArrayList<Integer> a = new ArrayList<>();
long p = 1;
for (int i = 1; i < n; i++) {
if(gcd(i, n) == 1) {
a.add(i);
p = (p * i) % n;
}
}
StringBuilder sb = new StringBuilder();
for(int i: a) {
if(p == n - 1 && p == i) continue;
sb.append(i).append(' ');
an++;
}
System.out.println(an);
System.out.println(sb);
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
String nextLine(){
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | ed88171089c229ca4b264f74dd0e894b | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | //package Practise;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class Product1ModuloN {
public static void main(String args[]) {
FastScanner fs=new FastScanner();
int n=fs.nextInt();
int []arr=new int[n];
long product=1;
for(int i=1;i<n;i++) {
//System.out.println(i+" "+gcd(i,n));
if(gcd(i,n)==1) {
arr[i]=1;
product=(product*i)%n;
}
}
//System.out.println("product "+product);
if(product!=1) {
arr[(int)product]=0;
}
int count=0;
for(int i=0;i<n;i++) {
if(arr[i]==1)
count++;
}
System.out.println(count);
for(int i=0;i<n;i++) {
if(arr[i]==1)
System.out.print(i+" ");
}
//System.out.println();
}
public static int gcd(int a,int b) {
if(b==0)
return a;
return gcd(b,a%b);
}
static final Random random=new Random();
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | a2d595abeeb65fb1135f3afc04cda302 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
public class Practice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextInt();
long product = 1;
ArrayList<Long> set = new ArrayList<>();
set.add(1L);
for(long i=2;i<n;i++){
long gcd = findGcd(i,n);
if(gcd==1){
set.add(i);
product = product * i;
product %= n;
}
}
if(product!=1) {
set.remove(product);
}
StringBuilder sb = new StringBuilder();
for(long x: set) {
sb.append(x);
sb.append(" ");
}
System.out.println(set.size());
System.out.println(sb.toString().trim());
}
private static long findGcd(long a, long b) {
if(b==0) return a;
return findGcd(b, a%b);
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 451d8f98d6d91960404b3ff1cc529a75 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
long prod = 1;
List<Long> output = new ArrayList<>();
for(long k = 1; k < n; k++) {
if(gcd(n,k) == 1) {
prod = (prod*k) % n;
output.add(k);
}
}
if(prod == 1) {
System.out.println(output.size());
for(long u: output) {
System.out.print(u + " ");
}
} else {
System.out.println(output.size() - 1 );
for(long u: output) {
if(u != (prod+n) % n) {
System.out.print(u + " ");
}
}
}
}
public static long gcd(long a, long b) {
if (b==0) return a;
return gcd(b,a%b);
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 485822625757012204aad0dcc94c24b8 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | /*
JAI MATA DI
*/
import java.util.*;
//import javax.print.attribute.HashAttributeSet;
import java.io.*;
import java.math.BigInteger;
import java.sql.Array;
public class CP {
static class FR{
BufferedReader br;
StringTokenizer st;
public FR() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static int mod = 1000000007;
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static boolean[] prime(int num) {
boolean[] bool = new boolean[num];
for (int i = 0; i< bool.length; i++) {
bool[i] = true;
}
for (int i = 2; i< Math.sqrt(num); i++) {
if(bool[i] == true) {
for(int j = (i*i); j<num; j = j+i) {
bool[j] = false;
}
}
}
if(num >= 0) {
bool[0] = false;
bool[1] = false;
}
return bool;
}
static class Pair implements Comparable<Pair>{
int x;
int y;
Pair(int key , int value){
this.x = key;
this.y = value;
}
@Override
public int compareTo(Pair o) {
return this.x - o.x;
}
// @Override
// public int hashCode(){
// return val + ind;
// }
}
public static void main(String args[]) {
FR sc = new FR();
int n = sc.nextInt();
ArrayList<Long> al = new ArrayList<>();
long prod = 1;
for(int i = 1; i <n-1 ; i++) {
if(gcd(i, n) == 1) {
prod *= i;
prod %= n;
al.add((long)i);
}
}
if(prod*(n-1)%n == 1) al.add((long)n-1);
StringBuilder sb = new StringBuilder();
for(long e:al) sb.append(e+" ");
System.out.println(al.size());
System.out.println(sb);
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 7cc4a32c6787c1db22e2d662229ec416 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;
public class Main {
private static final String NO = "NO";
private static final String YES = "YES";
InputStream is;
PrintWriter out;
String INPUT = "";
private static final long MOD = 1000000007;
private static final int MAXN = 100000;
void solve() {
int T = 1;//ni();
for (int i = 0; i < T; i++) {
solve(i);
}
}
void solve(int T) {
int n = ni();
boolean b[] = new boolean[n];
long ans = 1;
int cnt = 0;
for (int i = 1; i < n; i++) {
if (gcd(i, n) == 1) {
b[i] = true;
ans = (ans * i) % n;
cnt++;
}
}
if (ans != 1) {
b[(int) ans] = false;
cnt--;
}
out.println(cnt);
for (int i = 1; i < n; i++) {
if (b[i])
out.print(i + " ");
}
out.println();
}
// a^b
long power(long a, long b) {
long x = 1, y = a;
while (b > 0) {
if (b % 2 != 0) {
x = (x * y) % MOD;
}
y = (y * y) % MOD;
b /= 2;
}
return x % MOD;
}
private long gcd(long a, long b) {
while (a != 0) {
long tmp = b % a;
b = a;
a = tmp;
}
return b;
}
void run() throws Exception {
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
if (!INPUT.isEmpty())
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new Main().run();
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
// ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n) {
if (!(isSpaceChar(b)))
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private List<Integer> na2(int n) {
List<Integer> a = new ArrayList<Integer>();
for (int i = 0; i < n; i++)
a.add(ni());
return a;
}
private int[][] na(int n, int m) {
int[][] a = new int[n][];
for (int i = 0; i < n; i++)
a[i] = na(m);
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
private long[][] nl(int n, int m) {
long[][] a = new long[n][];
for (int i = 0; i < n; i++)
a[i] = nl(m);
return a;
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) {
System.out.println(Arrays.deepToString(o));
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 2df5014949e60deb207cfb3f2c97d9ce | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class MyClass
{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static final long mod=(long)1e9+7;
public static long pow(long a,int p)
{
long res=1;
while(p>0)
{
if(p%2==1)
{
p--;
res*=a;
res%=mod;
}
else
{
a*=a;
a%=mod;
p/=2;
}
}
return res;
}
public static void main(String args[])
{
FastReader fs=new FastReader();
PrintWriter pw=new PrintWriter(System.out);
int n=fs.nextInt();
if(n==2)
{
pw.println(1);
pw.println(1);
}
else
{
TreeSet<Integer> ts=new TreeSet<>();
for(int i=1;i<n;i++)
ts.add(i);
for(int i=2;i<=n;i++)
{
if(n%i==0&&ts.contains(i))
{
for(int j=i;j<n;j+=i)
ts.remove(j);
}
}
long prod=1;
for(int i:ts)
prod=(prod*i)%n;
if(prod!=1)
ts.pollLast();
pw.println(ts.size());
for(int i:ts)
pw.print(i+" ");
}
pw.flush();
pw.close();
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 26669e3a77ae66028bb9808a3b0c3928 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
import java.io.*;
import java.util.Map.Entry;
public class Main{
static final int inf=Integer.MAX_VALUE;
static final int mod=(int)1e9+7;
//divide into cases, brute force
//sort, greedy, binary search
//transform into graph
static void solve(Reader in, Writer out){
int n=in.ni();
TreeSet<Long> st=new TreeSet<>();
long pro=1;
for(long i=1; i<n; ++i){
if(gcd(i,n)==1){
st.add(i);
pro*=i;
pro%=n;
}
}
if(pro!=1){
st.remove(pro);
}
out.println(st.size());
for(long i: st){
out.print(i+" ");
}
out.println();
}
public static void main(String[] args) throws IOException {
Writer out=new Writer(System.out);
Reader in=new Reader(System.in);
int ts=1;
// ts=in.ni();
while(ts-->0) {
solve(in, out);
}
out.close();
}
static long ppow(long n, long m){
if(m==0) return 1;
long tmp=ppow(n,m/2);
tmp=tmp*tmp%mod;
return m%2==0 ? tmp : tmp*n %mod;
}
static long gcd(long n, long m){ return m==0 ? n : gcd(m, n%m); }
static long lcm(long n, long m){ return n/gcd(n,m)*m; }
static int smaller(long [] a, long k){
if(a[0]>k) return 0;
int t=0;
for(int j=a.length; j>=1; j/=2){
while(t+j<a.length && a[t+j]<k) t+=j;
}
return t+1;
}
static void reverse(int a[]){
ArrayList<Integer> al=new ArrayList<>();
for(int i: a) al.add(i);
Collections.reverse(al);
for(int i=0; i<al.size(); ++i) a[i]=al.get(i);
}
static void sort(long a[]) {
ArrayList<Long> al=new ArrayList<>();
for(long i: a) al.add(i);
Collections.sort(al);
for(int i=0; i<a.length; ++i) a[i]=al.get(i);
}
static void sort(pair [] p){
ArrayList<pair> pl=new ArrayList<>();
for(pair pa: p) pl.add(pa);
Collections.sort(pl, (p1, p2)->{
return Long.compare(p1.u, p2.u)==0 ? Long.compare(p1.v,p2.v): Long.compare(p1.u,p2.u);
});
for(int i=0; i<p.length; ++i) p[i]=pl.get(i);
}
static void reverse(pair[] p){
int n=p.length;
for(int i=0; i<p.length/2; ++i){
pair tmp=new pair(0,0);
tmp.copy(p[i]);
p[i].copy(p[n-1-i]);
p[n-1-i].copy(tmp);
}
}
static class pair{
long u, v;
pair(long g, long h){u=g; v=h;}
void copy(pair p){u=p.u; v=p.v;};
}
static class Reader{
BufferedReader br;
StringTokenizer to;
Reader(InputStream stream){
br=new BufferedReader(new InputStreamReader(stream));
to=new StringTokenizer("");
}
String nextLine() {
String line="";
try {
line=br.readLine();
}catch(IOException e) {};
return line;
}
String ns() {
while(!to.hasMoreTokens()) {
try {
to=new StringTokenizer(br.readLine());
}catch(IOException e) {}
}
return to.nextToken();
}
int ni() { return Integer.parseInt(ns()); }
long nl() { return Long.parseLong(ns()); }
int [] ra(int n) { int a[]=new int[n]; for(int i=0; i<n; i++) a[i]=ni(); return a; }
long [] rla(int n) { long [] a =new long[n]; for(int i=0; i<n; ++i) a[i]=nl(); return a; }
}
static class Writer extends PrintWriter{
Writer(OutputStream stream){ super(stream); }
void println(pair p){ println(p.u+" "+p.v); }
void println(int a[]){ for(int i: a) print(i+" "); println(); }
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 3bb5dfa46fc23ade79afc39f0cf18d72 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes |
import java.io.*;
import java.lang.*;
public final class CodeForces {
static FastScanner scan = new FastScanner();
static StringBuffer output = new StringBuffer();
static final long MOD = (long) (1e9 + 7);
public static void main(String[] args) throws IOException {
int t = 1;
//t = scan.nextInt();
while (t-- > 0) {
solve();
}
System.out.println(output);
}
static void solve() throws IOException {
int n = scan.nextInt();
boolean[] selected = new boolean[n];
int count = 0;
long product = 1;
for(int i=1;i<n;i++) {
if(gcd(i, n) == 1) {
selected[i] = true;
count ++;
product = (product * i) % n;
}
}
if(product != 1) {
selected[(int) product] = false;
count --;
}
output.append(count).append("\n");
for(int i=1;i<n;i++) {
if(selected[i])
output.append(i).append(" ");
}
}
static int gcd(int a, int b) {
return (b == 0) ? a : gcd(b, a%b);
}
static class FastScanner {
String line;
String[] values;
int position;
BufferedReader bufferedReader;
FastScanner() {
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
}
FastScanner(Boolean test) {
InputStream inputStream = CodeForces.class.getResourceAsStream("test.txt");
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
bufferedReader = new BufferedReader(inputStreamReader);
}
void readLine() throws IOException {
if (values == null || position == values.length) {
line = bufferedReader.readLine();
values = line.trim().split(" ");
position = 0;
}
}
String next() throws IOException {
readLine();
return values[position++];
}
Integer nextInt() throws IOException {
return Integer.parseInt(next());
}
Long nextLong() throws IOException {
return Long.parseLong(next());
}
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 63f05dc0796385897d0555ea2aba64b7 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
import java.io.*;
public class Product1Mod {
private static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static int gcd(int a, int b)
{
if(b==0)
return a;
return gcd(b,a%b);
}
public static void solution(int n)
{
ArrayList<Integer> list = new ArrayList<>();
for(int i = 1; i<=n-1; i++)
{
if(gcd(i,n)==1)
list.add(i);
}
long pro = 1;
for(int i = 0; i<list.size(); i++)
pro = (pro%n * list.get(i)%n)%n;
if(pro==1)
{
out.println(list.size());
for(int i = 0; i<list.size(); i++)
out.print(list.get(i)+" ");
return;
}
out.println(list.size()-1);
for(int i = 0; i<list.size(); i++)
{
if(list.get(i)!=pro)
out.print(list.get(i)+" ");
}
return;
}
private static PrintWriter out = new PrintWriter(System.out);
public static void main (String[] args)
{
MyScanner s = new MyScanner();
int n = s.nextInt();
solution(n);
out.flush();
out.close();
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 5c85ec38e951900ec31c99e7a4944e65 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static InputReader scn = new InputReader(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] HastaLaVistaLa) {
// Running Number Of TestCases (t)
int t = 1;
while(t-- > 0)
solve();
out.close();
}
public static void dummy(){
int n = scn.nextInt();
out.println(n);
}
static int max = 0;
static int[] dp;
static int MOD = 100;
public static void solve() {
// Main Solution (AC)
int n = scn.nextInt();
List<Integer> ans = new ArrayList<>();
for(int i = 1; i < n; i++) {
if(gcd(i, n) == 1) ans.add(i);
}
while(true) {
long prod = 1;
for(int i = 0; i < ans.size(); i++) {
prod *= ans.get(i);
prod %= n;
}
if(prod == 1) {
out.println(ans.size());
for(int i : ans) out.print(i + " ");
break;
}else ans.remove(ans.get(ans.size() - 1));
}
}
public static long binpow(long n, long m, long mod) {
long res = 1;
while (m > 0) {
if (m % 2 == 1) {
res *= n;
res %= mod;
m--;
}
n *= n;
n %= mod;
m /= 2;
}
return res;
}
public static HashMap<Integer, Integer> CountFrequencies(int[] arr) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i : arr) {
if (map.containsKey(i)) map.put(i, map.get(i) + 1);
else map.put(i, 1);
}
return map;
}
public static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
public static long sum(int[] arr) {
long sum = 0;
for(int i : arr) sum += i;
return sum;
}
// Number of Divisor for Range [l, r]
public static int[] NumberOFDivisors(int l, int r) {
int[] arr = new int[l - r + 1];
for(int i = l; i <= r; i++) {
for(int j = i; j <= r; j += i)
arr[j]++;
}
return arr;
}
public static int LowerBound(int a[], int x) { // x is the target value or key
int l = -1,r = a.length;
while(l + 1 < r) {
int m = (l + r) >>> 1;
if(a[m] >= x) r = m;
else l = m;
}
return r;
}
static boolean[] prime;
public static void sieveOfEratosthenes(int n) {
prime = new boolean[n+1];
for(int i=0;i<n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++) {
if(prime[p] == true) {
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
}
public static int UpperBound(long a[], long x) {// x is the key or target value
int l = -1,r = a.length;
while(l + 1 < r) {
int m = (l + r) >>> 1;
if(a[m] <= x) l = m;
else r = m;
}
return l + 1;
}
public static long lcm(long a, long b) {
return (a * b) / gcd(a, b);
}
public static void sort(int[] arr) {
Random rand = new Random();
int n = arr.length;
for(int i = 0; i<n; i++) {
swap(arr, i, rand.nextInt(n));
}
Arrays.sort(arr);
}
public static void swap(int[] arr, int i, int j) {
if(i!=j) {
arr[i] ^= arr[j];
arr[j] ^= arr[i];
arr[i] ^= arr[j];
}
}
public static void sortbyColumn(int[][] arr, int col) {
Arrays.sort(arr, new Comparator<int[]>() {
public int compare(final int[] entry1, final int[] entry2) {
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
});
}
public static void ArraySort2D(double[][] arr, int xy) {
// xy == 0, for sorting wrt X-Axis
// xy == 1, for sorting wrt Y-Axis
Arrays.sort(arr, Comparator.comparingDouble(o -> o[xy]));
}
public static int binarySearch(int arr[], int l, int r, int x) {
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int[] readIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scn.nextInt();
}
return a;
}
public long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = scn.nextLong();
}
return a;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return nextLine();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 39071726dcb42be67bd1a5c365a3f4f9 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class A{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
long MOD = 1000000007;
int t = 1;
outer: for(int z=0; z<t; z++) {
int n = sc.nextInt();
ArrayList<Integer> list = new ArrayList<>();
long val = 1;
for(int i=1; i<n; i++) {
if(gcd(i,n)==1) {
list.add(i);
val *= i;
val %= n;
}
}
if(val%n!=1) list.remove(list.size()-1);
System.out.println(list.size());
for(int i: list) System.out.print(i + " ");
}
sc.close();
}
public static int gcd(int a, int b) {
if(b==0) return a;
return gcd(b,a%b);
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 7ec4aa32f4c0a966a5e6234805dcb40c | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class Codeforces {
static FastScanner sc;
static PrintWriter out;
static int mod = (int) (1e9+7);
public static void main(String[] args){
sc = new FastScanner();
out = new PrintWriter(System.out);
int t = 1;
while(t-- > 0){
solve();
}
out.close();
}
private static void solve() {
int n = sc.nextInt();
ArrayList<Integer> arr = new ArrayList<>();
long product = 1l;
for(int i=1; i<n; i++){
if(findGcd(i, n) == 1) {
arr.add(i);
product %= n;
product *= i%n;
}
}
int val = (int) (product%n);
if(val != 1){
out.println(arr.size()-1);
for(int i=0; i<arr.size(); i++){
if(arr.get(i) == val)
continue;
out.print(arr.get(i) + " ");
}
}
else{
out.println(arr.size());
for(int i=0; i<arr.size(); i++){
out.print(arr.get(i) + " ");
}
}
out.println();
}
public static int nCr(int n, int k) {
int C[] = new int[k + 1];
// nC0 is 1
C[0] = 1;
for (int i = 1; i <= n; i++) {
// Compute next row of pascal
// triangle using the previous row
for (int j = Math.min(i, k); j > 0; j--)
C[j] = (C[j]%mod + C[j - 1]%mod)%mod;
}
return C[k]%mod;
}
private static int findLcm(int n, int m) {
return n*m/findGcd(Math.min(n, m), Math.max(n, m));
}
private static int findGcd(int n, int m) {
if(n == 0)
return m;
return findGcd(m%n, n);
}
private static long findLcm(long n, long m) {
return n*m/findGcd(Math.min(n, m), Math.max(n, m));
}
private static long findGcd(long n, long m) {
if(n == 0)
return m;
return findGcd(m%n, n);
}
public static void intSort (int[] arr) {
//First ruffle
int n = arr.length;
Random r = new Random();
for (int i=0; i<arr.length; i++) {
int j = r.nextInt(n);
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
//then sort
Arrays.sort(arr);
}
public static void longSort (long[] arr) {
//First ruffle
int n = arr.length;
Random r = new Random();
for (int i=0; i<arr.length; i++) {
int j = r.nextInt(n);
long temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
//then sort
Arrays.sort(arr);
}
public static void sort2dArray (long[][] arr) {
Arrays.sort(arr, (a, b) -> {
if (a[0] == b[0])
return (int) (a[1] - b[1]);
return (int) (a[0] - b[0]);
});
// Arrays.sort(arr, (a, b) -> {
// if (a[1] == b[1])
// return (int) (a[0] - b[0]);
// return (int) (a[1] - b[1]);
// });
}
public static void sort2dArray (int[][] arr) {
// Arrays.sort(arr, (a, b) -> {
// if (a[1] == b[1])
// return (int) (a[0] - b[0]);
// return (int) (a[1] - b[1]);
// });
Arrays.sort(arr, (a, b) -> {
if (a[0] == b[0])
return (int) (a[1] - b[1]);
return (int) (a[0] - b[0]);
});
}
public static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | 95a69ec31658b5ff5e84b9ccf1f19c74 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes | import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class C {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n=scan.nextInt();
long ans=1;
List<Integer>list=new ArrayList<>();
for(int i=1;i<n;i++){
if(gcd(i,n)==1){
list.add(i);
ans=ans*i%n;
}
}
if(ans!=1){
list.remove(list.size()-1);
ans=1;
for(int i=0;i<list.size();i++){
ans=ans*list.get(i)%n;
}
}
//System.out.println(ans);
System.out.println(list.size());
for(int x:list){
System.out.printf("%d ",x);
}
System.out.println();
}
private static int ksm(int a,int b,int mod){
if(b==1)return a%mod;
if(b==0)return 1%mod;
int ans=ksm(a,b/2,mod);
ans=ans*ans%mod;
if(b%2==1)ans=ans*a%mod;
return ans;
}
private static int gcd(int x,int y){
return y==0?x:gcd(y,x%y);
}
}
| Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output | |
PASSED | b4256e6d2794b97081c53a20bef14d36 | train_110.jsonl | 1618839300 | Now you get Baby Ehab's first words: "Given an integer $$$n$$$, find the longest subsequence of $$$[1,2, \ldots, n-1]$$$ whose product is $$$1$$$ modulo $$$n$$$." Please solve the problem.A sequence $$$b$$$ is a subsequence of an array $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deleting some (possibly all) elements. The product of an empty subsequence is equal to $$$1$$$. | 256 megabytes |
import java.io.*;
import java.util.*;
import java.lang.*;
public class Main {
static long mod=(long) Math.pow(10,9) + 7;
public static void main(String[] args) throws IOException {
FastReader sc = new FastReader();
StringBuilder fout = new StringBuilder();
int t =1;
while (t-- != 0) {
int k = sc.nextInt();
Solve(k);
}
/*System.out.println(fout);*/
}
public static void Solve(int n)
{
ArrayList<Integer> a=new ArrayList();
long val=1;
for(int i=1;i<n;i++)
{
if(gcd(i,n)==1)
{
val*=i;
val%=n;
a.add(i);
}
}
ArrayList<Integer> res=new ArrayList();
for(int i=0;i<a.size();i++)
{
if(a.get(i)==val && val!=1) continue;
res.add(a.get(i));
}
System.out.println(res.size());
for(int i=0;i<res.size();i++)
System.out.print(res.get(i)+" ");
}
static void no() {
System.out.println("No");
}
static void yes() {
System.out.println("Yes");
}
static long gcd(long a, long b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
public static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["5", "8"] | 1 second | ["3\n1 2 3", "4\n1 3 5 7"] | NoteIn the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$[1,2,3,4]$$$. Its product is $$$24$$$ which is congruent to $$$4$$$ modulo $$$5$$$. Hence, the answer is $$$[1,2,3]$$$. | Java 11 | standard input | [
"greedy",
"number theory"
] | d55afdb4a83aebdfce5a62e4ec934adb | The only line contains the integer $$$n$$$ ($$$2 \le n \le 10^5$$$). | 1,600 | The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. | standard output |
Subsets and Splits